DEV Community

Discussion on: Algorithm Practice: Two Sum

Collapse
 
snlgrg profile image
ѕυηιℓ gαяg

Here is a solution from My side..
for every element you traverse u put totalReq - current element into a Map or dictionary, for next time u will before putting this number search for this new element that u got in next iteration in Map or dictionary, if u found it then u got ur element
totalReq = 10
dictionary ;
temp;
for (elem : arr) {
temp = totalReq - elem;
if (dictionary has temp) {
print "you found req elem"
// also remove the elem or may be not if problem statement requires
} else {
put element in dic;
}
}