Skip to content

Instantly share code, notes, and snippets.

@RomainL972
Created October 12, 2020 00:28
Show Gist options
  • Save RomainL972/69ccf65ecc1b2de11f6aef73e9570391 to your computer and use it in GitHub Desktop.
Save RomainL972/69ccf65ecc1b2de11f6aef73e9570391 to your computer and use it in GitHub Desktop.
2-sum algorithm
array = [-2, 2, 4, 8, 9]
target = 13
def function(array, target):
for f in range(len(array)):
for s in range(f+1, len(array)-f):
if array[f] + array[s] == target:
return True
return False
def new(array, target):
complements = {}
for e in array:
comp = complements.get(e)
if comp:
return True
complements[target - e] = e
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment