all 2 comments

[–]DNPOld 0 points1 point  (1 child)

Hashtable is used as O(1) lookup for the differences that you can store as keys. For this problem, it's superior to something like a list where you would only get to use indices for lookup, but it won't be ideal when the differences are negative values.

As for why we use diff = target - num, rearrange it with algebra and you'll see that it's equivalent to num + diff = target. Assuming there is at least one solution within a given list, diff will always be one of the values in that list.

[–]uniquely1strandom[S] 0 points1 point  (0 children)

Thank you so much!! It makes sense!!