Easy · Hash Map
Understand → Visualise → Practice.
You are given an array and a target. Find the two values that add to the target.
Store seen values in a hash map and look up the complement.
Store number → index.
need = target − n.
If need exists, return the pair.
Brute force checks every pair: O(n²). The hash map solution is O(n) time and O(n) space.
Check the map before you store the current number, or a value could pair with itself.
Read the full guides first: Big-O and Complexity, Arrays and Dynamic Arrays, Prefix Sums and Difference Arrays.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.