Find the Triplet Sum Nearest to a Target Value
Solve this Problemnums and an integer target, find the sum of exactly three different elements that lands as close as possible to target. Return that sum itself — not the triplet. You may assume there's exactly one distance-minimizing sum to report.
For example, with nums = [-6, 1, 1, 1, 8] and target = 2, the triplet 1 + 1 + 1 = 3 is only 1 away from the target, closer than anything else the array can produce, so the answer is 3.
Test Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
3 ≤ nums.length ≤ 500 - ◆
-1000 ≤ nums[i] ≤ 1000 - ◆
-10⁴ ≤ target ≤ 10⁴
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
| 1 | class Solution { |
| 2 | public int closestTripletSum(int[] nums, int target) { |
| 3 | Arrays.sort(nums); |
| 4 | int n = nums.length; |
| 5 | int best = nums[0] + nums[1] + nums[2]; |
| 6 | for (int i = 0; i < n - 2; i++) { |
| 7 | int left = i + 1, right = n - 1; |
| 8 | while (left < right) { |
| 9 | int sum = nums[i] + nums[left] + nums[right]; |
| 10 | if (Math.abs(sum - target) < Math.abs(best - target)) { |
| 11 | best = sum; |
| 12 | } |
| 13 | if (sum == target) { |
| 14 | return sum; |
| 15 | } else if (sum < target) { |
| 16 | left++; |
| 17 | } else { |
| 18 | right--; |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | return best; |
| 23 | } |
| 24 | } |
| 25 |
-4nums = [-6, 1, 1, 1, 8], target = 2. It's already sorted. Seed best with the sum of the first three values: -6 + 1 + 1 = -4.
Approach & Solutions
Brute Force — Three Nested Loops
BruteTry every combination of three distinct indices, compute each sum, and keep whichever one lands closest to target (smallest absolute difference), updating the best answer as you go.
O(n³)O(1)1class Solution {
2 public int closestTripletSum(int[] nums, int target) {
3 int n = nums.length;
4 int best = nums[0] + nums[1] + nums[2];
5 for (int i = 0; i < n; i++) {
6 for (int j = i + 1; j < n; j++) {
7 for (int k = j + 1; k < n; k++) {
8 int sum = nums[i] + nums[j] + nums[k];
9 if (Math.abs(sum - target) < Math.abs(best - target)) {
10 best = sum;
11 }
12 }
13 }
14 }
15 return best;
16 }
17}Optimal — Sort + Two Pointer
OptimalSort the array, then fix the first number with a single loop and scan the rest with two pointers, exactly like the zero-sum-triplet search. At every step, check whether the current sum beats the best difference found so far. If the sum matches the target exactly, nothing can beat a difference of zero, so return immediately. Otherwise move left up when the sum is too small, or right down when it's too big — sorting is what lets a single comparison tell you which direction closes the gap.
O(n²)O(1) extra1class Solution {
2 public int closestTripletSum(int[] nums, int target) {
3 Arrays.sort(nums);
4 int n = nums.length;
5 int best = nums[0] + nums[1] + nums[2];
6 for (int i = 0; i < n - 2; i++) {
7 int left = i + 1, right = n - 1;
8 while (left < right) {
9 int sum = nums[i] + nums[left] + nums[right];
10 if (Math.abs(sum - target) < Math.abs(best - target)) {
11 best = sum;
12 }
13 if (sum == target) {
14 return sum;
15 } else if (sum < target) {
16 left++;
17 } else {
18 right--;
19 }
20 }
21 }
22 return best;
23 }
24}