Skip to content

Instantly share code, notes, and snippets.

@bgschiller
Last active December 27, 2015 07:49
Show Gist options
  • Save bgschiller/7291813 to your computer and use it in GitHub Desktop.
Save bgschiller/7291813 to your computer and use it in GitHub Desktop.
Three Elements that Sum to Zero solution
A = [-6, -8, 0, 3, 10, -8, 18]
def three_elems(A):
hashed_a = { val : index for (index, val) in enumerate(A) }
for i in range(len(A)):
for j in range(i+1,len(A)):
third_index = hashed_a.get(-1*(A[i] + A[j]))
if third_index is not None and third_index not in (i,j):
return (i,j,third_index)
return (-1,-1,-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment