1. Two Sum
Given an array of integers nums and an integer target, return the indices of the two numbers that add up to target. Exactly one solution exists.
def two_sum(nums, target):
seen = {} # value -> index
for i, n in enumerate(nums):
complement = target - n
if complement in seen:
return [seen[complement], i]
seen[n] = i
Brute force checks every pair (O(n^2)). Walking the array once while remembering what you've already seen turns it into a single O(n) pass: at each index, ask the hash map whether its complement already showed up.