Two Sum¶
Problem Description¶
Given an array of integers nums
and an integer target
, return indices of the two numbers such that they add up to target
.
Solution Approach¶
We use a hash map to store previously seen numbers and their indices. For each number, we:
- Calculate the complement (target - current number)
- Check if the complement exists in our hash map
- If found, return both indices
- If not, add current number and index to hash map
Complexity Analysis¶
- Time Complexity: O(n)
- Space Complexity: O(n)