-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.java
More file actions
32 lines (26 loc) · 823 Bytes
/
TwoSum.java
File metadata and controls
32 lines (26 loc) · 823 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
29
30
31
32
import java.util.HashMap;
/*
* Two SUM problem using a hash
*/
public class TwoSum {
public static void main(String[] args) {
int[] numbers = Utils.readIntArrayFromFile("data/HashInt.txt");
HashMap<Integer, Integer> table = new HashMap<Integer, Integer>();
for (int i : numbers) {
table.put(i, 1);
}
int count = 0;
for (int sum=2500; sum<=4000; sum++) {
boolean found = false;
for (int index=0; index<numbers.length; index++) {
if (table.containsKey(sum-numbers[index]) && ((sum-numbers[index]) != numbers[index])) {
found = true;
break;
}
}
if (found)
count++;
}
System.out.println(count);
}
}