Given two input arrays [-1, 8, 3]
and [3, 7, 2]
your function will return true if any two of the numbers in the first array add up to the any numbers in the second array.
\$-1 + 3 = 2 \therefore \text{True}\$
My algorithm simply takes all the pairs from the first input and check if they exist in second array set which the complexity of set lookup is \$O(1)\$. Overall complexity is \$O(n^2)\$ I'm pretty sure there should be a better algorithm leveraging hash tables or sorting and doing a binary search. Is there a way to make time complexity more efficient?
My code:
def arraySum(inputs, tests):
# iterating through all possible sums and check if it exists in test by using a test set which
#makes the complexity better with O(1) lookup in set.
my_set=set(tests)
for i in range(len(inputs)-1):
for j in range(i+1,len(inputs)):
my_sum=inputs[i]+inputs[j]
if my_sum in my_set:
return True
return False #this will take care of an edge case when there is one input from each arr