Skip to content

Commit

Permalink
Merge pull request #78 from yanurag1414/Triplet_Sum
Browse files Browse the repository at this point in the history
Adding triplet sum code
  • Loading branch information
Chitresh-code authored Oct 24, 2023
2 parents 4d66e97 + 4a7d90e commit 1a9a2c2
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions DSA_Codesheet/Java/Triplet_Sum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.Arrays;

public class Triplet_Sum {
public static void main(String[] args) {
int[] arr = {1,2,4,3,6};
System.out.println(threeSum(arr,10));

}
static boolean threeSum(int[] arr,int x){
Arrays.sort(arr);
int n = arr.length;
for (int i = 0; i < n; i++) {
int j = i+1;
int k = arr.length-1;
while (j<k){
int sum = arr[i]+arr[j]+arr[k];
if(sum==x){
return true;
} else if (sum<x) {
j++;
}else{
k--;
}
}
}
return false;
}
}

0 comments on commit 1a9a2c2

Please sign in to comment.