Find two numbers that add up to the target using a hashmap
The naive approach compares all pairs (i, j) where i < j, yielding n(n-1)/2 = O(n²) comparisons. The hash table approach exploits the fact that for each element x, its complement target - x is uniquely determined—reducing search from O(n) to O(1).
We trade the nested loop's multiplicative cost for a single loop with constant-time hash operations, transforming O(n²) into O(n).
In the worst case, we traverse the entire array before finding the solution (or determine no solution exists). At that point, the hash table contains all n elements. The space-time tradeoff is explicit: we invest O(n) space to reduce O(n) lookups from O(n) each to O(1) each.
The hash table serves as a "memory" of previously seen elements, enabling instant complement lookups instead of re-scanning the array.