[Solved] 3 Sum closest -16 C++, Java, Python – Microsoft, Google , Amazon, Media.net

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.

You may assume that each input would have exactly one solution.

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).

Constraints:

  • 3 <= nums.length <= 500
  • -1000 <= nums[i] <= 1000
  • -104 <= target <= 104

Solution

Intuition

Sorting + Two Pointers

Complexity

  • Time complexity: O(NlogN + N^2) ~ O(N^2)
  • Space complexity: O(1)
class Solution {
public:
    int threeSumClosest(vector<int>& nums, int target) {
        int mindiff = INT_MAX;
        int n = nums.size();
        sort(nums.begin(), nums.end());
        int ans = 0;

        for(int i = 0; i < n; i++){
            int j = i + 1;
            int k = n - 1;

            while(j < k){
                int sum = nums[i] + nums[j] + nums[k];

                if(sum == target) return target;
                else{
                    int diff = abs(target - sum);

                    if(diff < mindiff){
                        mindiff = diff;
                        ans = sum;
                    }
                }

                if(sum < target) j++;
                else if(sum > target) k--;
            }
        }
        return ans;
    }
};
class Solution {
    public int threeSumClosest(int[] nums, int target) {
        int mindiff = Integer.MAX_VALUE;
        int n = nums.length;
        Arrays.sort(nums);
        int ans = 0;

        for(int i =0; i<n; i++){
            int j = i+1;
            int k = n-1;

            while(j<k){
                    int sum = nums[i]+nums[j]+nums[k];

                    if(sum == target) return target;

                    else{
                        int diff = Math.abs(target - sum);

                        if(diff<mindiff){
                            mindiff = diff;
                            ans = sum;
                        }
                    }

                    if(sum<target) j++;
                    else if(sum>target) k--;
                }
            
        }
        return ans;
    }
}
class Solution:
    def threeSumClosest(self, nums: List[int], target: int) -> int:
        mindiff = float('inf')
        nums.sort()
        n = len(nums)
        ans = 0

        for i in range(n):
            j = i + 1
            k = n - 1

            while j < k:
                sum_val = nums[i] + nums[j] + nums[k]

                if sum_val == target:
                    return target
                else:
                    diff = abs(target - sum_val)

                    if diff < mindiff:
                        mindiff = diff
                        ans = sum_val

                if sum_val < target:
                    j += 1
                elif sum_val > target:
                    k -= 1

        return ans
Share your love

Leave a Reply

Your email address will not be published. Required fields are marked *