Two Sum
Medium · Algorithms
Checking every pair of numbers takes two nested loops. A dict makes it one pass: for each number, look up whether its partner (target - number) was already seen, and remember where you saw each number.
Write two_sum(nums, target) that returns the indices [i, j] (with i < j) of the two numbers that add up to target. Each test has at most one such pair. You may not use the same index twice. If there is no pair, return [].
Next: Days Between Two Dates