Skip to content

Commit

Permalink
Merge pull request ephremdeme#177 from ishaangupta-YB/patch-2
Browse files Browse the repository at this point in the history
Create Two Pointer Algorithm
  • Loading branch information
ephremdeme authored Oct 2, 2020
2 parents 3e7d5ea + 68737e4 commit 150458a
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Algorithms/Two Pointer Algorithm
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.Arrays;

public class TwoPointerAlgo {

public static void main(String[] args) {
int arr[] = {0,-1,3,-4,5,-3,4,1};

int n = arr.length;
int x=0,k=0,j=n-1;

Arrays.sort(arr); //Sorting is necessary

for(int i=0;i<n-2;i++) {
k=i+1;
x=-arr[i];
j=n-1;

while(k<j) {
if(arr[k] + arr[j]<x) {
k++;
}else if(arr[k] +arr[j] > x) {
j--;
}else {
System.out.println(arr[i] + " "+ arr[k]+" "+arr[j]);
k++;
j--;
}
}
}
}

}

0 comments on commit 150458a

Please sign in to comment.