Complexity analysis, discussion about best solution
Anonymous
Given 2 arrays of numbers and a number, find 1 number from each array that sum up to the 3rd given input: You have two arrays, array1 and array2, and you want a in array1 and b in array2 such that a + b = c. The trick is to rewrite this as b = c - a. We transform each element a in array1 to c - a, then check for collisions in the two arrays. We can do this via a hash map or by sorting the two arrays and stepping through them much like in the merge part of merge sort. Python code: def main(array1, array2, c): mem = dict() for a in array1: mem[c - a] = True for b in array2: if b in mem: return c - b, b return None
Check out your Company Bowl for anonymous work chats.