-
Notifications
You must be signed in to change notification settings - Fork 0
/
FourNumberSum.java
33 lines (32 loc) · 1008 Bytes
/
FourNumberSum.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
import java.util.*;
class Program {
// Average: O(n^2) time | O(n^2) space
// Worst: O(n^3) time | O(n^2) space
public static List<Integer[]> fourNumberSum(int[] array, int targetSum) {
// Write your code here.
List<Integer[]> quadruplets = new ArrayList<>();
Map<Integer, List<Integer[]>> pairSums = new HashMap<>();
for (int i = 1; i < array.length - 1; i++) {
int k = i;
for (int l = k + 1; l < array.length; l++) {
int currSum = array[k] + array[l];
int diff = targetSum - currSum;
if (pairSums.containsKey(diff)) {
for (Integer[] pair : pairSums.get(diff)) {
Integer[] quadruplet = { pair[0], pair[1], array[k], array[l] };
quadruplets.add(quadruplet);
}
}
}
for (int j = 0; j < i; j++) {
int currSum = array[i] + array[j];
Integer[] pair = { array[i], array[j] };
if (!pairSums.containsKey(currSum)) {
pairSums.put(currSum, new ArrayList<>());
}
pairSums.get(currSum).add(pair);
}
}
return quadruplets;
}
}