-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from yanurag1414/Triplet_Sum
Adding triplet sum code
- Loading branch information
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |