Skip to content

Commit 150458a

Browse files
authored
Merge pull request ephremdeme#177 from ishaangupta-YB/patch-2
Create Two Pointer Algorithm
2 parents 3e7d5ea + 68737e4 commit 150458a

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Algorithms/Two Pointer Algorithm

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.Arrays;
2+
3+
public class TwoPointerAlgo {
4+
5+
public static void main(String[] args) {
6+
int arr[] = {0,-1,3,-4,5,-3,4,1};
7+
8+
int n = arr.length;
9+
int x=0,k=0,j=n-1;
10+
11+
Arrays.sort(arr); //Sorting is necessary
12+
13+
for(int i=0;i<n-2;i++) {
14+
k=i+1;
15+
x=-arr[i];
16+
j=n-1;
17+
18+
while(k<j) {
19+
if(arr[k] + arr[j]<x) {
20+
k++;
21+
}else if(arr[k] +arr[j] > x) {
22+
j--;
23+
}else {
24+
System.out.println(arr[i] + " "+ arr[k]+" "+arr[j]);
25+
k++;
26+
j--;
27+
}
28+
}
29+
}
30+
}
31+
32+
}

0 commit comments

Comments
 (0)