By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
"This single problem tests sorting, two-pointer technique, and edge-case handling—master it, and you prove you can optimize brute-force solutions into linear time, a skill that separates L4 from L5 engineers at FAANG."
Before tackling Three Sum, ensure you understand: 1. Sorting & Two-Pointer Technique – How to traverse a sorted array from both ends to find pairs efficiently. 2. Hash Sets for Lookup – How to use sets to track seen values (though not always optimal for Three Sum). 3. Time Complexity Analysis – Why brute-force is O(n³) and how to reduce it to O(n²).
nums = [-1,0,1,2,-1,-4]
[-4,-1,-1,0,1,2]
nums[i] == nums[i-1]
i
nums[i] > 0
Follow these steps every time you see a Three Sum problem:
Time: O(n log n), Space: O(1) (or O(n) if sorting is not in-place).
Iterate with a Fixed Pointer (i)
i = 0
n-3
For each i, treat nums[i] as the first element of the triplet.
nums[i]
Skip Duplicates for i
Example: [-1, -1, 0, 1] → Skip the second -1.
[-1, -1, 0, 1]
-1
Two-Pointer Search (left, right)
left = i + 1
right = n - 1
While left < right:
left < right
current_sum = nums[i] + nums[left] + nums[right]
current_sum == 0
current_sum < 0
left++
current_sum > 0
right--
Skip Duplicates for left/right
left
right
Example: [-2, 0, 0, 2, 2] → Skip the second 0 and 2.
[-2, 0, 0, 2, 2]
0
2
Early Termination (Optional)
If nums[i] > 0, break (since array is sorted, no possible triplet).
Return Result
Problem: Given an array nums, return all unique triplets [nums[i], nums[j], nums[k]] such that i != j != k and nums[i] + nums[j] + nums[k] == 0.
nums
[nums[i], nums[j], nums[k]]
i != j != k
nums[i] + nums[j] + nums[k] == 0
Input: nums = [-1, 0, 1, 2, -1, -4] Output: [[-1, -1, 2], [-1, 0, 1]]
nums = [-1, 0, 1, 2, -1, -4]
[[-1, -1, 2], [-1, 0, 1]]
Sort the array: [-4, -1, -1, 0, 1, 2]
[-4, -1, -1, 0, 1, 2]
Iterate with fixed i:
nums[i] = -4
left = 1
right = 5
current_sum = -4 + (-1) + 2 = -3
current_sum = -4 + 0 + 2 = -2
current_sum = -4 + 1 + 2 = -1
left == right
i = 1
nums[i] = -1
left = 2
current_sum = -1 + (-1) + 2 = 0
[-1, -1, 2]
nums[left] == nums[left+1]
left = 3
current_sum = -1 + 0 + 1 = 0
[-1, 0, 1]
nums[right] == nums[right-1]
right = 3
i = 2
i=1
i = 3 (nums[i] = 0):
i = 3
nums[i] = 0
Result: [[-1, -1, 2], [-1, 0, 1]]
def threeSum(nums): nums.sort() res = [] n = len(nums) for i in range(n - 2): if i > 0 and nums[i] == nums[i - 1]: continue # Skip duplicates left, right = i + 1, n - 1 while left < right: total = nums[i] + nums[left] + nums[right] if total < 0: left += 1 elif total > 0: right -= 1 else: res.append([nums[i], nums[left], nums[right]]) # Skip duplicates while left < right and nums[left] == nums[left + 1]: left += 1 while left < right and nums[right] == nums[right - 1]: right -= 1 left += 1 right -= 1 return res
Problem: Same as above, but the input may have more than 3 duplicates (e.g., [-1, -1, -1, 0, 1, 1]).
[-1, -1, -1, 0, 1, 1]
Input: nums = [-1, -1, -1, 0, 1, 1] Output: [[-1, 0, 1]]
nums = [-1, -1, -1, 0, 1, 1]
[[-1, 0, 1]]
python while left < right and nums[left] == nums[left + 1]: left += 1 while left < right and nums[right] == nums[right - 1]: right -= 1
def threeSum(nums): nums.sort() res = [] n = len(nums) for i in range(n - 2): if i > 0 and nums[i] == nums[i - 1]: continue left, right = i + 1, n - 1 while left < right: total = nums[i] + nums[left] + nums[right] if total < 0: left += 1 elif total > 0: right -= 1 else: res.append([nums[i], nums[left], nums[right]]) # Skip ALL duplicates while left < right and nums[left] == nums[left + 1]: left += 1 while left < right and nums[right] == nums[right - 1]: right -= 1 left += 1 right -= 1 return res
Problem: Given an array nums and a target target, find three integers such that their sum is closest to target. Return the sum.
target
Input: nums = [-1, 2, 1, -4], target = 1 Output: 2 (because -1 + 2 + 1 = 2 is closest to 1)
nums = [-1, 2, 1, -4]
target = 1
-1 + 2 + 1 = 2
1
[-4, -1, 1, 2]
closest_sum = infinity
closest_sum
|-3 - 1| < |closest_sum - 1|
current_sum < target
current_sum = -1 + 1 + 2 = 2
current_sum > target
def threeSumClosest(nums, target): nums.sort() closest_sum = float('inf') n = len(nums) for i in range(n - 2): left, right = i + 1, n - 1 while left < right: current_sum = nums[i] + nums[left] + nums[right] if abs(current_sum - target) < abs(closest_sum - target): closest_sum = current_sum if current_sum < target: left += 1 elif current_sum > target: right -= 1 else: return current_sum # Early exit if exact match return closest_sum
sum < target
sum > target
"Alright, let’s lock this in. Three Sum is all about sorting, two pointers, and skipping duplicates. Here’s the playbook: 1. Sort the array—this lets you use two pointers. 2. Loop with a fixed i, treating nums[i] as the first number. 3. Skip duplicates for i to avoid repeats. 4. Use two pointers (left, right) to find pairs that sum to -nums[i]. 5. Skip duplicates for left and right after finding a valid triplet. 6. Early termination if nums[i] > 0 (since the array is sorted). Time complexity? O(n²). Space? O(1) if you ignore sorting. Now go crush it—you’ve got this!
-nums[i]
This framework is directly usable in interviews—memorize the steps, and you’ll solve Three Sum problems with confidence. ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.