forked from thevanshjain/DAA
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path3sum.java
More file actions
37 lines (33 loc) · 1.33 KB
/
Copy path3sum.java
File metadata and controls
37 lines (33 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// link- https://workat.tech/problem-solving/practice/three-sum
class Solution {
List<List<Integer>> threeSum(int[] nums) {
// add your logic here
int target = 0;
ArrayList<List<Integer>> sol = new ArrayList<List<Integer>>();
if(nums == null || nums.length == 0){
return sol;
}
Arrays.sort(nums);
for(int i = 0 ; i<=nums.length-1 ;i++){
int remaining = target - nums[i] ;
int front = i+1;
int back = nums.length-1;
while(front < back){
int twoSum = nums[front] + nums[back];
if(twoSum < remaining)front++;
else if(twoSum >remaining)back--;
else{
List<Integer> triplet = new ArrayList<>();
triplet.add(nums[i]);
triplet.add(nums[front]);
triplet.add(nums[back]);
sol.add(triplet);
while(front < back && nums[front] == triplet.get(1))front++;
while(front < back && nums[back] == triplet.get(2))back--;
}
}
while( i+1<=nums.length-1 && nums[i] == nums[i+1]) i++;
}
return sol;
}
}