3Sum
Expert Answer & Key Takeaways
A complete guide to understanding and implementing Two Pointers.
3Sum
Given an integer array
nums, return all the unique triplets [nums[i], nums[j], nums[k]] such that i \neq j, i \neq k, and j \neq k, and nums[i] + nums[j] + nums[k] == 0.Visual Representation
Sorted Array: [-4, -1, -1, 0, 1, 2]
Step 1: Fix i = -1 (Index 1)
L = -1 (Index 2), R = 2 (Index 5)
Sum = -1 + (-1) + 2 = 0 -> FOUND! [-1, -1, 2]
Step 2: L moves to 0 (Index 3), R moves to 1 (Index 4)
Sum = -1 + 0 + 1 = 0 -> FOUND! [-1, 0, 1]Examples
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
The triplets are (-1, 0, 1) and (-1, -1, 2).
Approach 1
Level I: Brute Force (Check All Triplets)
Intuition
Check every possible combination of three numbers to see if they sum to zero. To avoid duplicate triplets, we can sort each triplet and store it in a Set.
⏱ O(N^3)💾 O(k) where k is the number of triplets
Detailed Dry Run
| i | j | k | nums[i] | nums[j] | nums[k] | Sum | Result |
| :--- | :--- | :--- | :--- | :--- | :--- | :--- |
| 0 | 1 | 2 | -1 | 0 | 1 | 0 | FOUND! [-1, 0, 1] |
⚠️ Common Pitfalls & Tips
O(N^3) complexity is too slow for N > 1000.
Approach 2
Level II: Better (Sorting + Binary Search)
Intuition
If we sort the array and fix two elements
nums[i] and nums[j], we need to find -(nums[i] + nums[j]) in the remaining part of the array. Since it is sorted, we can use binary search.- Sort the array.
- Fix two pointers
iandj. - Search for the required third value using binary search in the range
(j+1, n-1).
⏱ O(N^2 log N)💾 O(1)
Detailed Dry Run
Input:
[-1, 0, 1, 2, -1, -4]. Sorted: [-4, -1, -1, 0, 1, 2]i=-4, j=-1: Search for5? Not found.i=-1, j=-1: Search for2? FOUND at index 5. Result:[-1, -1, 2].i=-1, j=0: Search for1? FOUND at index 4. Result:[-1, 0, 1].
⚠️ Common Pitfalls & Tips
O(N^2 log N) is better than O(N^3) but still not as good as the O(N^2) two-pointer solution.
Approach 3
Level III: Optimal (Sort + Two Pointers)
Intuition
To optimize, we sort the array first. This allows us to use the Two Pointers technique for the 'Two Sum' part of the problem.
- Fix the first element
nums[i]. - Use Two Pointers (
L = i+1, R = n-1) to find pairs that sum to-nums[i]. - Skip duplicate values for
i,L, andRto ensure unique triplets.
⏱ O(N^2)💾 O(1) (excluding sorting space)
Detailed Dry Run
Input:
[-1, 0, 1, 2, -1, -4]. Sorted: [-4, -1, -1, 0, 1, 2]| i | val[i] | L | R | Sum | Action |
|---|---|---|---|---|---|
| 0 | -4 | 1 (-1) | 5 (2) | -3 | Sum < 0, L++ |
| 1 | -1 | 2 (-1) | 5 (2) | 0 | FOUND! L++, R-- |
| 1 | -1 | 3 (0) | 4 (1) | 0 | FOUND! L++, R-- |
⚠️ Common Pitfalls & Tips
Remember to skip duplicate values for all three pointers to avoid duplicate triplets in the result.
Course4All Technical Board
Verified ExpertSenior Software Engineers & Algorithmic Experts
Our DSA content is authored and reviewed by engineers from top tech firms to ensure optimal time and space complexity analysis.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.