Given an array of integers and a target, return the indices of the two numbers that add up to target. Exactly one solution; can't reuse an element.
Do you spot that a nested loop (O(n²)) can become one pass by remembering what you've seen?
Scan once. For each number x, the partner you need is target − x. Keep a map of value → index; if the partner was already seen, you're done. This is the archetypal 'trade space for time' move: the map turns the inner search from O(n) to O(1), collapsing the whole thing from O(n²) to O(n). Store value→index (not just presence) so you can return indices. Check for the partner BEFORE inserting the current number — otherwise a target like 6 with a lone 3 would falsely match itself. The same 'have I already seen the complement?' reflex powers duplicate detection, pair-with-difference, and two-sum's many variants.
The moment you catch yourself writing a nested loop to find a pair or a match, ask: 'could a hash map remember this for me?'
Time O(n) · Space O(n)