Problem 1
Given a list of numbers, return whether any two sums 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?
function checkSums(numbers, k) { | |
const possibleSolutions = new Set() | |
for (let number of numbers) { | |
if (possibleSolutions.has(number)) return true | |
possibleSolutions.add(k - number) | |
} | |
return false | |
} | |
// checkSums([10, 15, 3, 7], 17) -> true | |
// checkSums([1, 2, 3, 4], 8) -> false |