-
Notifications
You must be signed in to change notification settings - Fork 0
/
P19.java
40 lines (37 loc) · 1.25 KB
/
P19.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
import java.io.*;
import java.util.*;
public class P19 {
public static List<int[]> brute_force(int[] arr, int s) {
ArrayList<int[]> res = new ArrayList<>();
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] + arr[j] == s) {
res.add(new int[] { arr[i], arr[j] });
} else if (arr[i] + arr[j] > s)
j = arr.length;
}
}
return res;
}
public static List<int[]> using_space(int[] arr, int s) {
ArrayList<int[]> res = new ArrayList<>();
HashMap<Integer, Integer> hm = new HashMap<>();
for (int a : arr)
hm.put(a, hm.getOrDefault(a, 0) + 1);
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
if (hm.containsKey(s - arr[i])) {
int count = hm.get(s - arr[i]);
for (int j = 0; j < count; j++)
res.add(new int[] { arr[i], s - arr[i] });
hm.remove(arr[i]);
}
}
return res;
}
public static List<int[]> pairSum(int[] arr, int s) {
return brute_force(arr, s);
// return using_space(arr,s);
}
}