Created
October 12, 2020 00:28
-
-
Save RomainL972/69ccf65ecc1b2de11f6aef73e9570391 to your computer and use it in GitHub Desktop.
2-sum algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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