Search code examples
algorithmarray-algorithms

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.


Solution

  • 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