Math Fundamentals18 sections · 814 units
Open in Course

Two Sum - Algorithm

(Single pass with map)

1.1. Create an empty hash map

2.2. For each element arr[i]: compute complement = target - arr[i]

3.3. If complement exists in map, return [map[complement], i]

4.4. Otherwise, store arr[i] -> i in map

One loop. O(1) lookups. Total: O(n).