3Sum Closest
3Sum Closest is LeetCode 16 (Medium). Given an integer array nums and an integer target, choose three numbers at different positions whose sum is as close to target as possible, and return that sum.
- Each input has exactly one closest sum, so ties never need breaking.
- With up to 500 numbers there are about 2 × 10⁷ triples to try one by one; sorting first brings the work down to about 125,000 sums.
- 3 <= nums.length <= 500
- -1000 <= nums[i] <= 1000
- -10⁴ <= target <= 10⁴
Intuition
Sort the array and fix the first number of the triple, nums[i]. The other two come from the part to its right: lo starts at its left end and hi at its right end.
In a sorted array, comparing the sum with the target tells you which pointer to move:
- Sum below target: move
loright, the only way to raise it. Every pair ofnums[lo]with a partner left ofhiis even smaller, so none of them can be closer. - Sum above target: move
hileft, for the mirror reason. - Sum equal to target: distance 0 cannot be beaten, so return it.
Each move rules out only pairs that cannot win, so the closest triplet sum is never skipped.
Pick numbers whose sum is near a value, in an array you are free to sort: fix one number and close two pointers from both ends.
Approach
Before reading on, sort [-1, 2, 1, -4], fix -4 and put pointers on the next number and the last one. If their sum is below the target, which pointer can bring it closer? Aim for O(n²).
Sort the array
Sort nums in ascending order. Sorted order is what lets a single comparison with target decide which pointer to move.
Start from a real triple
Set best = nums[0] + nums[1] + nums[2]. Starting from an actual sum means there is always a valid answer to compare against, with no infinity placeholder whose distance to the target can overflow.
Fix one number, sweep the other two
For each i from 0 to n − 3, set lo = i + 1 and hi = n − 1. While lo < hi, compute the sum of the three and keep it as best if |sum − target| is smaller than |best − target|.
Move a pointer, or stop
If the sum is below target, add 1 to lo; if it is above, subtract 1 from hi. If it equals target, return it straight away.
Code in Python, C++ and Java
lo starts at the smallest of them and hi at the largest.hi left would only shrink it, so every pair of −1 with a smaller partner is even further away. Drop −1 by moving lo right.hi left would only shrink it, so every pair of 1 with a smaller partner is even further away. Drop 1 by moving lo right.lo starts at the smallest of them and hi at the largest.hi left.lo starts at the smallest of them and hi at the largest.hi left would only shrink it, so every pair of 0 with a smaller partner is even further away. Drop 0 by moving lo right.lo starts at the smallest of them and hi at the largest.hi left would only shrink it, so every pair of −5 with a smaller partner is even further away. Drop −5 by moving lo right.hi left would only shrink it, so every pair of −4 with a smaller partner is even further away. Drop −4 by moving lo right.hi left.hi left.Common pitfalls
Comparing signed differences
if s - target < best - target:
best = sif abs(s - target) < abs(best - target):
best = sWithout abs, a sum far below the target looks like an improvement. For [-1, 2, 1, -4] with target 1 this returns −4 instead of 2, because −4 − 1 = −5 is the smallest signed difference.
Edge cases
There is only one triple. best starts as its sum, the loop runs once for i = 0 and checks the same triple again, and that sum is returned.
Complexity
i, the two pointers meet after at most n steps.3Sum Closest FAQ
How is this different from 3Sum?
3Sum (LeetCode 15) lists every triple that adds up to exactly 0 and must skip repeated values to avoid duplicate triples. Here only one sum is returned, so repeated values do no harm, and the scan tracks a distance instead of collecting matches.
Does skipping repeated values of nums[i] help?
It saves a little time on inputs with many duplicates, because the same first number would give the same sweep. It is optional: the answer is the same with or without it.