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
X in A, perform a binary search for I-X. If I-X is in A, we have a solution.This is O(nlogn).
If A contains integers in a given (small enough) range, we can use a trick to make it O(n):
V. For each element X in A, we increment V[X].V[X] we also check if V[I-X] is >0. If it is, we have a solution.