-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProblem1.java
More file actions
30 lines (25 loc) · 723 Bytes
/
Copy pathProblem1.java
File metadata and controls
30 lines (25 loc) · 723 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
/***
* This problem was recently asked by Google.
*
* Given a list of numbers and a number k, return whether any two numbers from the list add up to k.
*
* For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.
*
* Bonus: Can you do this in one pass?
*/
import java.util.HashSet;
import java.util.Set;
public class Problem1 {
public static void main(String[] args) {
System.out.println(contains(107));
}
static boolean contains(int k) {
Set<Integer> set = new HashSet<>();
int[] list = {10,15,3,7,100,200};
for (int i : list) {
if (set.contains(k - i)) return true;
set.add(i);
}
return false;
}
}