03 - Two Sum
Given an integer array nums and an integer target, return the indices i and j such that:
Conditions:
-
i != j -
Exactly one valid pair exists
-
Return the smaller index first
Examples¶
Example 1
Input
Output
Explanation
nums[0] + nums[1] = 3 + 4 = 7
Example 2
Input
Output
Explanation
nums[0] + nums[2] = 4 + 6 = 10
Example 3
Input
Output
Solution¶
Java Code (Optimal O(n) Approach)¶
import java.util.HashMap;
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[]{map.get(complement), i};
}
map.put(nums[i], i);
}
return new int[]{};
}
}
Intuition¶
The key observation:
Which means:
So while iterating through the array:
-
For each number
nums[i] -
Compute the complement needed:
-
Check if that complement already exists in the data structure.
-
If it exists → we found the required pair.
Example:
Iteration:
| i | nums[i] | complement | map | result |
|---|---|---|---|---|
| 0 | 3 | 4 | {} | store 3 |
| 1 | 4 | 3 | {3:0} | found pair |
Return:
Java Collections Framework Component Used¶
HashMap¶
What it is¶
HashMap is a key-value based data structure from the Java Collections Framework.
It allows fast lookup, insertion, and deletion using hashing.
Structure used in this problem:
Example map:
Why HashMap is used¶
HashMap allows us to check if the required complement already exists in constant time.
Without a map:
-
Brute force requires two nested loops
-
Time complexity becomes O(n²)
With HashMap:
-
Lookup is O(1)
-
Overall complexity becomes O(n)
How it is used in the solution¶
1. Creating the Map¶
Stores:
2. Finding the Complement¶
This calculates the value needed to reach the target.
3. Checking if Complement Exists¶
If true:
-
The complement was seen earlier
-
The pair is found.
4. Returning the Answer¶
We return the stored index and the current index.
This automatically keeps the smaller index first because the earlier index was inserted first.
5. Storing Elements¶
Each element is stored for future complement checks.
Complexity Analysis¶
Time Complexity¶
Each element is processed once.
HashMap operations (put, get, containsKey) are O(1) on average.
Space Complexity¶
In the worst case, all elements may be stored in the HashMap.
Key Takeaways¶
- Transform the equation:
into
-
Use
HashMapto store number → index. -
Check complement before inserting current element.
-
This reduces the problem from O(n²) → O(n).