Problem Description
Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers.
Examples
Example 1: Input: nums = [-1,2,1,-4], target = 1 Output: 2 Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). Example 2: Input: nums = [0,0,0], target = 1 Output: 0 Explanation: The sum that is closest to the target is 0. (0 + 0 + 0 = 0).
Python Solution
def threeSumClosest(nums: List[int], target: int) -> int:
nums.sort()
n = len(nums)
closest_sum = float('inf')
for i in range(n - 2):
left, right = i + 1, n - 1
while left < right:
current_sum = nums[i] + nums[left] + nums[right]
if current_sum == target:
return current_sum
if abs(current_sum - target) < abs(closest_sum - target):
closest_sum = current_sum
if current_sum < target:
left += 1
else:
right -= 1
return closest_sum
Java Solution
class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int n = nums.length;
int closestSum = Integer.MAX_VALUE;
for (int i = 0; i < n - 2; i++) {
int left = i + 1;
int right = n - 1;
while (left < right) {
int currentSum = nums[i] + nums[left] + nums[right];
if (currentSum == target) {
return currentSum;
}
if (closestSum == Integer.MAX_VALUE ||
Math.abs(currentSum - target) < Math.abs(closestSum - target)) {
closestSum = currentSum;
}
if (currentSum < target) {
left++;
} else {
right--;
}
}
}
return closestSum;
}
}
C++ Solution
class Solution {
public:
int threeSumClosest(vector& nums, int target) {
sort(nums.begin(), nums.end());
int n = nums.size();
int closestSum = INT_MAX;
for (int i = 0; i < n - 2; i++) {
int left = i + 1;
int right = n - 1;
while (left < right) {
int currentSum = nums[i] + nums[left] + nums[right];
if (currentSum == target) {
return currentSum;
}
if (closestSum == INT_MAX ||
abs(currentSum - target) < abs(closestSum - target)) {
closestSum = currentSum;
}
if (currentSum < target) {
left++;
} else {
right--;
}
}
}
return closestSum;
}
};
JavaScript Solution
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var threeSumClosest = function(nums, target) {
nums.sort((a, b) => a - b);
const n = nums.length;
let closestSum = Infinity;
for (let i = 0; i < n - 2; i++) {
let left = i + 1;
let right = n - 1;
while (left < right) {
const currentSum = nums[i] + nums[left] + nums[right];
if (currentSum === target) {
return currentSum;
}
if (Math.abs(currentSum - target) < Math.abs(closestSum - target)) {
closestSum = currentSum;
}
if (currentSum < target) {
left++;
} else {
right--;
}
}
}
return closestSum;
};
C# Solution
public class Solution {
public int ThreeSumClosest(int[] nums, int target) {
Array.Sort(nums);
int n = nums.Length;
int closestSum = int.MaxValue;
for (int i = 0; i < n - 2; i++) {
int left = i + 1;
int right = n - 1;
while (left < right) {
int currentSum = nums[i] + nums[left] + nums[right];
if (currentSum == target) {
return currentSum;
}
if (closestSum == int.MaxValue ||
Math.Abs(currentSum - target) < Math.Abs(closestSum - target)) {
closestSum = currentSum;
}
if (currentSum < target) {
left++;
} else {
right--;
}
}
}
return closestSum;
}
}
Complexity Analysis
- Time Complexity: O(n²) where n is the length of the input array
- Space Complexity: O(1) as we only use constant extra space
Solution Explanation
This solution uses the two-pointer technique after sorting the array. Here's how it works:
- First, we sort the array to make it easier to use the two-pointer approach
- For each number (i), we:
- Use two pointers (left and right) to find pairs that give sum closest to target-nums[i]
- Keep track of the closest sum found so far
- Move pointers based on comparison with target
Key points:
- Sorting helps make the two-pointer approach efficient
- We update closest sum whenever we find a closer sum to target
- The two-pointer technique reduces the time complexity from O(n³) to O(n²)
- We handle edge cases properly