3Sum — Turning Three Numbers Into a Two Sum Problem
LeetCode 15: fix one value, search for its opposite, and make duplicate handling visible.
What if finding three numbers could become a sequence of Two Sum searches?
3Sum asks for every unique triplet of values that sums to zero, using three different indices. I independently identified the transformation B + C = −A and developed an initial implementation. I needed assistance to complete duplicate handling and boundary checks. The accepted solution below includes those corrections.
Follow one fixed value in the animation. Keep the original array in view, compare it with the sorted version, and watch when a zero-sum triplet becomes part of the result.
Turn three numbers into two
B + C = −A
Original: [-1, 0, 1, 2, -1, -4]. Sorted: [-4, -1, -1, 0, 1, 2].
Unique triplets: [[-1,-1,2],[-1,0,1]].
Interactive playback requires JavaScript. The complete trace remains readable below.
Read all 25 steps
| Step | i / j / k | Target / pair sum / total | Decision | Unique triplets so far |
|---|---|---|---|---|
| 1 | — / — / — | — / — / — | nums.sort()Sort by value. The original input stays visible above; pointer indices refer to the sorted array. | [] |
| 2 | 0 / 1 / 5 | 4 / 1 / -3 | j = i + 1; k = len(nums) - 1Fix A = -4. Search to its right for B + C = 4. | [] |
| 3 | 0 / 1 / 5 | 4 / 1 / -3 | total = nums[i] + nums[j] + nums[k]Total is below zero. Move j right to try a larger pair sum. | [] |
| 4 | 0 / 2 / 5 | 4 / 1 / -3 | j += 1Move j one position right. Evaluate the new pair next. | [] |
| 5 | 0 / 2 / 5 | 4 / 1 / -3 | total = nums[i] + nums[j] + nums[k]Total is below zero. Move j right to try a larger pair sum. | [] |
| 6 | 0 / 3 / 5 | 4 / 2 / -2 | j += 1Move j one position right. Evaluate the new pair next. | [] |
| 7 | 0 / 3 / 5 | 4 / 2 / -2 | total = nums[i] + nums[j] + nums[k]Total is below zero. Move j right to try a larger pair sum. | [] |
| 8 | 0 / 4 / 5 | 4 / 3 / -1 | j += 1Move j one position right. Evaluate the new pair next. | [] |
| 9 | 0 / 4 / 5 | 4 / 3 / -1 | total = nums[i] + nums[j] + nums[k]Total is below zero. Move j right to try a larger pair sum. | [] |
| 10 | 0 / 5 / 5 | 4 / — / — | j += 1Move j one position right. Evaluate the new pair next. | [] |
| 11 | 0 / 5 / 5 | 4 / — / — | j < k → Falsej and k have met or crossed. The search for A = -4 is complete. | [] |
| 12 | 1 / 2 / 5 | 1 / 1 / 0 | j = i + 1; k = len(nums) - 1Fix A = -1. Search to its right for B + C = 1. | [] |
| 13 | 1 / 2 / 5 | 1 / 1 / 0 | total = nums[i] + nums[j] + nums[k]Total is zero. Record these three values once. | [] |
| 14 | 1 / 2 / 5 | 1 / 1 / 0 | result.append([nums[i], nums[j], nums[k]])Record [-1, -1, 2]. Next, move both pointers and skip repeated pair values. | [[-1,-1,2]] |
| 15 | 1 / 3 / 4 | 1 / 1 / 0 | j += 1; k -= 1Move both pointers inward. Check boundaries before skipping repeated values. | [[-1,-1,2]] |
| 16 | 1 / 3 / 4 | 1 / 1 / 0 | total = nums[i] + nums[j] + nums[k]Total is zero. Record these three values once. | [[-1,-1,2]] |
| 17 | 1 / 3 / 4 | 1 / 1 / 0 | result.append([nums[i], nums[j], nums[k]])Record [-1, 0, 1]. Next, move both pointers and skip repeated pair values. | [[-1,-1,2],[-1,0,1]] |
| 18 | 1 / 4 / 3 | 1 / — / — | j += 1; k -= 1Move both pointers inward. Check boundaries before skipping repeated values. | [[-1,-1,2],[-1,0,1]] |
| 19 | 1 / 4 / 3 | 1 / — / — | j < k → Falsej and k have met or crossed. The search for A = -1 is complete. | [[-1,-1,2],[-1,0,1]] |
| 20 | 2 / — / — | 1 / — / — | continueSkip repeated A = -1; the previous fixed value already searched its pairs. | [[-1,-1,2],[-1,0,1]] |
| 21 | 3 / 4 / 5 | 0 / 3 / 3 | j = i + 1; k = len(nums) - 1Fix A = 0. Search to its right for B + C = 0. | [[-1,-1,2],[-1,0,1]] |
| 22 | 3 / 4 / 5 | 0 / 3 / 3 | total = nums[i] + nums[j] + nums[k]Total is above zero. Move k left to try a smaller pair sum. | [[-1,-1,2],[-1,0,1]] |
| 23 | 3 / 4 / 4 | 0 / — / — | k -= 1Move k one position left. Evaluate the new pair next. | [[-1,-1,2],[-1,0,1]] |
| 24 | 3 / 4 / 4 | 0 / — / — | j < k → Falsej and k have met or crossed. The search for A = 0 is complete. | [[-1,-1,2],[-1,0,1]] |
| 25 | — / — / — | — / — / — | return resultSearch complete. Return 2 unique triplets. | [[-1,-1,2],[-1,0,1]] |
Why three pointers work
A + B + C = 0
B + C = −AAfter sorting, i fixes A. Set j = i + 1 and k to the last index, so the pair search only uses positions to the right of i. If the pair sum is below −A, move j right to try a larger value. If it is above −A, move k left to try a smaller one. Sorted order makes those eliminations safe.
When the total is zero, record the three values, move both searching pointers inward, then skip repeated left and right values. Before starting another search, skip a fixed value equal to its predecessor. These checks prevent repeated triplets without removing duplicate values that may be needed: [0, 0, 0] is valid when there are three zeroes at different indices.
The boundary j < k keeps the pair’s indices distinct. When they meet or cross, that fixed value is finished. The outer loop leaves room for two more elements. The answer contains values, not their original indices; the animation sorts a copy so you can still see the input order.
My accepted Python solution
This is the code I supplied for the accepted submission, including the duplicate-skipping loops and their boundary checks. It sorts nums in place. The animation follows the same decisions and keeps snapshots for rewinding.
class Solution:
def threeSum(self, nums: list[int]) -> list[list[int]]:
nums.sort()
result = []
for i in range(len(nums) - 2):
# Skip duplicate fixed elements
if i > 0 and nums[i] == nums[i - 1]:
continue
j = i + 1
k = len(nums) - 1
while j < k:
total = nums[i] + nums[j] + nums[k]
if total == 0:
result.append([nums[i], nums[j], nums[k]])
j += 1
k -= 1
# Skip duplicate left values
while j < k and nums[j] == nums[j - 1]:
j += 1
# Skip duplicate right values
while j < k and nums[k] == nums[k + 1]:
k -= 1
elif total < 0:
j += 1
else:
k -= 1
return resultSorting costs O(n log n), followed by at most a linear pair scan for each fixed position: O(n²) overall. The scan uses constant pointer state, but Python’s sort may use O(n) workspace; returned triplets need additional storage. The teaching trace has its own history storage.
What I needed help to finish
I had the reduction B + C = −A and an initial implementation. Completing the solution required assistance with duplicate handling and boundary checks; I did not independently derive every detail of the accepted code.
The final code handles repetition at two levels: skip a repeated fixed value before searching, then skip repeated pair values after recording a match. Both pair-skip loops check j < k before accessing their comparison positions. The animation separates recording, moving both pointers and skipping duplicates, so those actions do not disappear into one jump.
Compare [0, 0, 0, 0] with [-2, 0, 0, 2, 2]. Each has one unique answer, even though several combinations of indices can produce it. Duplicates in the input are allowed; duplicate value triplets in the output are not.
Problem and accepted submission
- Platform
- LeetCode
- Problem number
- 15
- Problem difficulty
- Medium
- Pattern
- Two Pointers — Fix One, Search Two
- Solution language
- Python 3
Accepted · 316 / 316 test cases · 678 ms · 22.40 MB · 18 November 2026. These are the supplied submission figures, not measurements of the animation or a performance benchmark.
Python sorting implementation notes describe the temporary workspace used by the sort.
Revisit Two Sum II for the sorted pair search, or Container With Most Water to compare a different reason for moving a pointer.