[Solution] numbers-sum-to-k | Facebook Interview Question
This problem was recently asked by Facebook: You are given a list of numbers, and a target number k. Return whether or not there are two numbers in the list that add up to k.
[4, 7, 1 , -3, 2] and
k = 5, return true since
4 + 1 = 5.
Try to do it in a single pass of the list.
Idea behind the solution
The idea is to pass on the array and add each number to a set of numbers. For each number
m we are checking we need to verify if the number
k - m exists in the set. If exists, then there are 2 numbers that sums to
The solution takes O(1) to insert the number in the set and O(n) to pass through the array.
Please check the Solution.java snippet for the solution.
This solution originally posted at: Github by @Murillo2380