Watch 10 video solutions for Two Sum, a easy level problem involving Array, Hash Table. This walkthrough by NeetCode has 1,640,483 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input: nums = [3,2,4], target = 6 Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6 Output: [0,1]
Constraints:
2 <= nums.length <= 104-109 <= nums[i] <= 109-109 <= target <= 109Follow-up: Can you come up with an algorithm that is less than
O(n2) time complexity?Problem Overview: Given an integer array nums and a target value, return the indices of two numbers whose sum equals the target. Each input has exactly one valid pair, and you cannot reuse the same element twice. The challenge is finding that pair efficiently without checking every possible combination.
Approach 1: Brute Force (O(n^2) time, O(1) space)
The most direct solution checks every possible pair of numbers. Use two nested loops: the outer loop picks the first number and the inner loop scans the rest of the array for a second number that satisfies nums[i] + nums[j] == target. Once the pair is found, return their indices. This approach works for any array and requires no extra data structures, so the space complexity stays O(1). The downside is the time complexity: with n elements, you potentially evaluate n(n-1)/2 pairs, resulting in O(n^2) time. It’s useful as a baseline during interviews because it demonstrates clear reasoning before optimizing.
Approach 2: Hash Map (O(n) time, O(n) space)
The optimal solution uses a hash table to track numbers you have already seen while scanning the array once. For each element nums[i], compute its complement target - nums[i]. Check if that complement already exists in the hash map. If it does, you found the required pair and can immediately return the stored index and the current index. If it does not, store the current number and its index in the map and continue iterating.
The key insight is that instead of searching the array repeatedly, the hash map allows constant-time lookups. Each iteration performs a hash lookup and possibly an insertion, both averaging O(1). Because the array is scanned once, the total time complexity becomes O(n). The tradeoff is extra memory: the map may store up to n elements, leading to O(n) space complexity.
This technique is a classic pattern when solving array problems involving complements, frequencies, or fast membership checks. Variants of this idea appear in many interview questions such as Two Sum II, Subarray Sum Equals K, and other problems involving prefix sums and lookups.
Recommended for interviews: Interviewers usually expect the hash map solution because it reduces the runtime from O(n^2) to O(n). Start by explaining the brute force approach to show you understand the problem space, then optimize using a hash map. Demonstrating that transition—from pair comparison to constant-time lookups—shows strong algorithmic thinking.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force (Nested Loops) | O(n^2) | O(1) | Baseline approach or when input size is very small and simplicity is preferred |
| Hash Map Lookup | O(n) | O(n) | General case; best choice when fast lookups are needed for complement values |