-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththreesome.java
83 lines (68 loc) · 2.72 KB
/
threesome.java
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
package aryanhere.Striver;
import java.util.ArrayList;
import java.util.Arrays;
public class threesome {
static ArrayList<ArrayList<Integer>> thresome(int nums[]){
ArrayList<ArrayList<Integer>> ans = new ArrayList<>(0);
//declaring the arrayllistt...
//Traversing witht the help of three loops and finding tht three elements...!
for(int i = 0; i < nums.length; i++){
for(int j = i + 1 ; j < nums.length; j++ ){
for(int k = j + 1; k < nums.length; k++){
// declaring one more arraylist to store the three elem
ArrayList<Integer> temp = new ArrayList<>();
if(nums[i] + nums[j] + nums[k] == 0){
temp.add(nums[i]);
temp.add(nums[j]);
temp.add(nums[k]);
}// pushing back the elements into ans arraylist..
if(temp.size() != 0){
ans.add(temp);
}
}
}
}
return ans;
// three loops so its obvious tht it will take around (O(N^3)) time complexity..!
// space complexity u know thth....^_^
// Lets optimize ittt..!
}
public static ArrayList<ArrayList<Integer>> threeSum(int[] arr) {
Arrays.sort(arr);
ArrayList<ArrayList<Integer>> res = new ArrayList<>();
for (int i = 0; i < arr.length ; i++) {
if (i == 0 || (i > 0 && arr[i] != arr[i - 1])) {
int low = i + 1, high = arr.length - 1;
int sum = -arr[i];
while (low < high) {
if (arr[low] + arr[high] == sum) {
ArrayList<Integer> temp = new ArrayList<>();
temp.add(arr[i]);
temp.add(arr[low]);
temp.add(arr[high]);
res.add(temp);
while (low < high && arr[low] == arr[low + 1]) low++;
while (low < high && arr[high] == arr[high - 1]) high--;
low++;
high--;
} else if (arr[low] + arr[high] < sum) {
low++;
} else {
high--;
}
}
}
}
return res;
}
public static void main(String[]args){
int arr[] = {-1 , 0 , 1 , 2 , -1 , -4 };
ArrayList<ArrayList<Integer>> ans = thresome(arr);
for(int i = 0 ; i < ans.size(); i++){
for(int j = 0 ; j < ans.get(i).size(); j++){
System.out.print(ans.get(i).get(j) + " ");
}
}
System.out.println();
}
}