forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FourSum.java
59 lines (52 loc) · 1.9 KB
/
FourSum.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
import java.util.*;
public class FourSum {
static class Pair {
int first, second;
Pair(int first, int second) {
this.first = first;
this.second = second;
}
}
/*
* Returns a list containing 4 numbers present at different indexes which sum to target. If there are multiple solutions,
* return any solution.
* If there is no solution, return an empty list.
*/
static List<Integer> findSubset(int[] nums, int target) {
List<Integer> result = new ArrayList<>();
// For every pair of elements, store their sum
Map<Integer, Pair> map = new HashMap<>();
int n = nums.length;
for (int i=0; i<n; i++) {
for (int j=i+1; j<n; j++) {
map.put(nums[i]+nums[j], new Pair(i, j));
}
}
for (int i=0; i<n; i++) {
for (int j=i+1; j<n; j++) {
int remainingSum = target - (nums[i] + nums[j]);
if (map.containsKey(remainingSum)) {
Pair pair = map.get(remainingSum);
if (pair.first != i && pair.first != j && pair.second != i && pair.second != j) {
result.addAll(Arrays.asList(nums[i], nums[j], nums[pair.first], nums[pair.second]));
return result;
}
}
}
}
return new ArrayList<Integer>();
}
public static void main(String[] args) {
int target = 23;
int[] nums = {10, 2, 3, 4, 5, 9, 7, 8};
List<Integer> result = findSubset(nums, target);
if (result.size() != 4) {
System.out.println("Four elements with the given sum not found in the array!");
} else {
for (int num : result) {
System.out.print(num + " ");
}
System.out.println();
}
}
}