forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFourSum.java
More file actions
83 lines (67 loc) · 2.23 KB
/
FourSum.java
File metadata and controls
83 lines (67 loc) · 2.23 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
package Hashing;
import java.util.*;
/**
* Author - archit.s
* Date - 28/10/18
* Time - 3:23 PM
*/
public class FourSum {
class Pair{
int key;
int value;
public Pair(int key, int value) {
this.key = key;
this.value = value;
}
}
public ArrayList<ArrayList<Integer>> fourSum(ArrayList<Integer> A, int B) {
Collections.sort(A);
ArrayList<ArrayList<Integer>> r = new ArrayList<>();
Map<ArrayList<Integer>, Boolean> result = new HashMap<>();
Map<Integer,List<Pair>> map = new HashMap<>();
for(int i=0;i<A.size()-1;i++){
for(int j=i+1;j<A.size();j++){
int tempSum = A.get(i) + A.get(j);
if(map.containsKey(tempSum)){
List<Pair> t = map.get(tempSum);
for (Pair p : t) {
if (p.key != i && p.value != i && p.key != j && p.value != j) {
ArrayList<Integer> temp = new ArrayList<>();
temp.add(A.get(p.key));
temp.add(A.get(p.value));
temp.add(A.get(i));
temp.add(A.get(j));
Collections.sort(temp);
result.put(temp, true);
}
}
}
List<Pair> t;
if(map.containsKey(B-tempSum)){
t = map.get(B-tempSum);
}
else{
t = new ArrayList<>();
}
Pair p = new Pair(i,j);
t.add(p);
map.put(B-tempSum, t);
}
}
ArrayList<ArrayList<Integer>> list = new ArrayList<>(result.keySet());
list.sort((a1, a2) -> {
int v = 0;
for (int i = 0; i < 4; i++) {
v += (a1.get(i)-a2.get(i));
if(v!=0){
return v;
}
}
return v;
});
return list;
}
public static void main(String[] args) {
System.out.println(new FourSum().fourSum(new ArrayList<>(Arrays.asList(1,0,-1,0,-2,2)),0));
}
}