Wednesday, April 13, 2011

Pairing


Give an unsorted array of integers A and and an integer I, find out if any two members of A add up to I.
For example:
A = < 3, 25, 9, 15>
I = 12 returns true
but I = 19 returns false.
Can you find the answer in O(n*log(n)) time?
Can you find the answer in O(n) time?

O(n):
-Create array B by subtracting A[i] from I for each i
-If B[i] exists in A return true.

No comments: