-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreeSumProblem.java
More file actions
86 lines (67 loc) · 2.42 KB
/
Copy pathThreeSumProblem.java
File metadata and controls
86 lines (67 loc) · 2.42 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package array;
import java.util.*;
/*
* Given an integer array nums, return all the triplets such that nums[i] + nums[j] + nums[k] = 0 for
* distinct i, j, k
* Notice that the solution set must not contain duplicate triplets
*
* Note:
* To Find: a[i] + a[j] + a[k] = 0;
* Thus, a[i] + a[j] = - a[k], find two sum which is equals to - a[k]
*
* */
public class ThreeSumProblem {
public static void main(String[] args) {
int[] arr = {4, 2, -1, -3, 0, 1, 2, 3};
int[] arr1 = {4, 2, 2, 2, 1, 1, 1, -1, -3, 0, 1, 2, 3, 3, 3, 3};
Arrays.sort(arr);
List<List<Integer>> triplets = new ArrayList<>();
for (int i = 0; i < arr.length - 2; i++) {
if (i > 0 && arr[i] == arr[i - 1]) {
continue;
}
/* *
* finding the sum which is negative of ith element i.e ( -a[k] = a[i] + a[j] )
* { -3, -1, 0, 1, 2, 2, 3, 4}
* for -3, return the two number which sum is 3 and compute from { -1, 0, 1, 2, 2, 3, 4 }
*/
List<List<Integer>> temp = twoSum(arr, -arr[i], i + 1);
for (List<Integer> list : temp) {
list.addFirst(arr[i]);
triplets.add(list);
}
}
System.out.println(triplets);
}
static List<List<Integer>> twoSum(int[] arr, int target, int startFrom) {
int left = startFrom;
int right = arr.length - 1;
List<List<Integer>> result = new ArrayList<>();
while (left < right) {
//skipping when the next element is same as previous element to reduce computation
if (left > startFrom && arr[left] == arr[left - 1]) {
left++;
continue;
}
//skipping when the next element is same as previous element to reduce computation
if (right < arr.length - 1 && arr[right] == arr[right + 1]) {
right--;
continue;
}
int sum = arr[left] + arr[right];
if (sum > target) {
right--;
} else if (sum < target) {
left++;
} else {
List<Integer> temp = new ArrayList<>();
temp.add(arr[left]);
temp.add(arr[right]);
result.add(temp);
left++;
right--;
}
}
return result;
}
}