Suppose we have a list A that contains integers in it.
A = [2,4,5,6,7]
Then given a target integer t
t = 6
I'd like to find an index tuple of two elements which makes t by summing up.
return [0,1]
I had used my python and coded like below:
def myMethod(A,t):
for i in range(len(A)):
for j in range(i,len(A)):
if A[i] + A[j] == target:
return([i,j])
This works, but some one told me there are better method only takes O(n) complexity on average.
How could it be possible?
Any hint?