def threeSumClosest(nums, target):
nums.sort()
closest_sum = float('inf')
for i in range(len(nums) - 2):
left = i + 1
right = len(nums) - 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
nums = [-1, 2, 1, -4]
target = 1
print(threeSumClosest(nums, target))
Explanation:
The "3Sum Closest" problem asks to find the sum of three integers in the array that is closest to the target sum.
The Python, Java, and C++ solutions follow a similar approach:
Sort the array in ascending order.
Initialize a variable closestSum to store the closest sum found so far, initially set to the sum of the first three elements.
Iterate over the array, fixing the first element of the potential triplet (nums[i]).
Use two pointers, left and right, to find the other two elements.
Calculate the current sum as nums[i] + nums[left] + nums[right].
Compare the absolute difference of the current sum and the target with the absolute difference of the closest sum and the target. Update closestSum if the current sum is closer.
If the current sum is less than the target, increment the left pointer to increase the sum.
If the current sum is greater than the target, decrement the right pointer to decrease the sum.
Repeat the above steps for all elements in the array.
Return the closest sum found.
The time complexity of the solution is O(n^2), where n is the number of elements in the array.
I hope this explanation helps! Let me know if you have any further questions.
import java.util.Arrays;
class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int closestSum = nums[0] + nums[1] + nums[2];
for (int i = 0; i < nums.length - 2; i++) {
int left = i + 1;
int right = nums.length - 1;
while (left < right) {
int 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;
}
public static void main(String[] args) {
int[] nums = {-1, 2, 1, -4};
int target = 1;
Solution solution = new Solution();
System.out.println(solution.threeSumClosest(nums, target));
}
}
Explanation:
The "3Sum Closest" problem asks to find the sum of three integers in the array that is closest to the target sum.
The Python, Java, and C++ solutions follow a similar approach:
Sort the array in ascending order.
Initialize a variable closestSum to store the closest sum found so far, initially set to the sum of the first three elements.
Iterate over the array, fixing the first element of the potential triplet (nums[i]).
Use two pointers, left and right, to find the other two elements.
Calculate the current sum as nums[i] + nums[left] + nums[right].
Compare the absolute difference of the current sum and the target with the absolute difference of the closest sum and the target. Update closestSum if the current sum is closer.
If the current sum is less than the target, increment the left pointer to increase the sum.
If the current sum is greater than the target, decrement the right pointer to decrease the sum.
Repeat the above steps for all elements in the array.
Return the closest sum found.
The time complexity of the solution is O(n^2), where n is the number of elements in the array.
I hope this explanation helps! Let me know if you have any further questions.
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int threeSumClosest(vector& nums, int target) {
sort(nums.begin(), nums.end());
int closestSum = nums[0] + nums[1] + nums[2];
for (int i = 0; i < nums.size() - 2; i++) {
int left = i + 1;
int right = nums.size() - 1;
while (left < right) {
int currentSum = nums[i] + nums[left] + nums[right];
if (currentSum == target) {
return currentSum;
}
if (abs(currentSum - target) < abs(closestSum - target)) {
closestSum = currentSum;
}
if (currentSum < target) {
left++;
} else {
right--;
}
}
}
return closestSum;
}
int main() {
vector nums = {-1, 2, 1, -4};
int target = 1;
cout << threeSumClosest(nums, target) << endl;
return 0;
}
Explanation:
The "3Sum Closest" problem asks to find the sum of three integers in the array that is closest to the target sum.
The Python, Java, and C++ solutions follow a similar approach:
Sort the array in ascending order.
Initialize a variable closestSum to store the closest sum found so far, initially set to the sum of the first three elements.
Iterate over the array, fixing the first element of the potential triplet (nums[i]).
Use two pointers, left and right, to find the other two elements.
Calculate the current sum as nums[i] + nums[left] + nums[right].
Compare the absolute difference of the current sum and the target with the absolute difference of the closest sum and the target. Update closestSum if the current sum is closer.
If the current sum is less than the target, increment the left pointer to increase the sum.
If the current sum is greater than the target, decrement the right pointer to decrease the sum.
Repeat the above steps for all elements in the array.
Return the closest sum found.
The time complexity of the solution is O(n^2), where n is the number of elements in the array.
I hope this explanation helps! Let me know if you have any further questions.