First there seems to lot of questions about this kind of problem but I couldn't find one that helps me.
I am trying find pairs of target with O(n)
or linear time complexity.
int[] input = {1, 6, 3, 2, 5, 5, 7, 8, 4, 8, 2, 5, 9, 9, 1};
int target = 10;
Expected output (in any order):
(1,9)(1,9)(6,4)(3,7)(2,8)(2,8)(5,5)(5,5)(5,5)(8,2)(8,2)(9,1)(9,1)
I have tried bruteforce approach with two for loops and it gives me right output but O(n*n)
.
for (int i = 0; i < input.length; i++) {
for (int j = i + 1; j < input.length; j++) {
if (input[i] + input[j] == target) {
System.out.println("(" + input[i] + "," + input[j] + ")");
}
}
}
Then I have tried the hashing which does not give all pairs. Two pairs are missing
Set<Integer> set = new HashSet<>();
for (int num : input) {
set.add(num);
if (set.contains(target - num)) {
System.out.println("(" + (target - num) + "," + num + ")");
}
}
Output:
(5,5)(5,5)(3,7)(2,8)(6,4)(2,8)(8,2)(5,5)(1,9)(1,9)(9,1)
I tried other approach which gave more pairs than expected
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < input.length; i++) {
map.put(input[i], i);
}
for (int num : input) {
if (map.containsKey((target - num))) {
System.out.println("(" + num + "," + (target - num) + ")");
}
}
Output:
(1,9)(6,4)(3,7)(2,8)(5,5)(5,5)(7,3)(8,2)(4,6)(8,2)(2,8)(5,5)(9,1)(9,1)(1,9)