C. Parks
C. Parks

Reputation: 63

Given 2 unsorted arrays and a sum, give two numbers that, when added, equal the sum

In these arrays, the numbers can be either positive or negative. Only one number from each array may be used.

I received this question as an algorithms question on a phone interview and it stumped me. The interviewer seemed to believe there was an O(n) solution.

Edit: My question is different than the "possible duplicates" because this question involves 2 arrays, rather than one.

Upvotes: 0

Views: 84

Answers (1)

MBo
MBo

Reputation: 80187

For unsorted arrays - fill hash table with the first array values and walk through the second one, checking if Sum-B[i] exists in the table

Upvotes: 6

Related Questions