-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion13.java
More file actions
28 lines (25 loc) · 887 Bytes
/
question13.java
File metadata and controls
28 lines (25 loc) · 887 Bytes
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
import java.util.HashSet;
public class question13{
public static void findPairs(int[] arr, int target) {
HashSet<Integer> seen = new HashSet<>();
HashSet<String> output = new HashSet<>();
for (int num : arr) {
int complement = target - num;
if (seen.contains(complement)) {
int min = Math.min(num, complement);
int max = Math.max(num, complement);
String pair = min + ", " + max;
if (!output.contains(pair)) {
System.out.println("(" + min + ", " + max + ")");
output.add(pair);
}
}
seen.add(num);
}
}
public static void main(String[] args) {
int[] arr = {1, 5, 7, -1, 5};
int target = 6;
findPairs(arr, target);
}
}