I saw a interview question as follows: Give an unsorted array of integers A and and an integer I, find out if any two members of A add up to I.
any clues?
ti
O(n) time and O(1) space
If the array is sorted there is a solution in O(n) time complexity.
Suppose are array is
array = {0, 1, 3, 5, 8, 10, 14}
And our x1 + x2 = k = 13, so output should be= 5, 8
array[ptr1] + array[ptr2]
if sum > k then decrement ptr2 else increment ptr1
Same thing explained in detail here. Seems like an Amazon interview Question http://inder-gnu.blogspot.com/2007/10/find-two-nos-in-array-whose-sum-x.html