You are given array of integers and value k. Find all pairs of numbers from the input array which sum equals to k.
There are several way in which this puzzle can be solved. One of them is to first sort the numbers and then to check if the sum of first and last value in the array is equal to k. If the sum is formed by these two values is smaller than k then you move your left index by one to larger number. If sum however is greater than k, then you move your right index. We print the sum and move both left and right index if sum is equal to k. Note that this solution does not use extra memory and overall finds the answer in O(nlogn), that is because sorting takes O(nlogn), visiting all values from left and right is O(n).
Another solution is to use hash table. For each value if check if there is corresponding value in hash table that would form a sum. If not you add value to hash table and continue. If the pair is found you print both numbers and remove them from hash table. This apporach gives us O(n) running time but requires additional O(n) space for hash table.