3Sum Closest in C++



The 3 Sum Closest problem involves finding the sum of three numbers in an array that is closest to a given target value.

We are given an integer array nums of length n and an integer target. Our goal is to find three integers in the array such that their sum is as close as possible to the target using C++ program.

In this task, we assume that each input has exactly one solution. If there are multiple sums equally close to the target, we return the maximum one.

Let's consider the following example scenario to understand the problem more clearly:

Scenario 1

Input: nums[] = [-1,2,1,-4], target = 1
Output: 2
Explanation: The triple sum will be [-1, 2, 1], which will give the closest value 2.

Scenario 2

Input: nums[] = [-1,2,2,4], target = 4
Output: 5
Explanation: Below are the all possible triplets:
[-1, 2, 2], sum = -1 + 2 + 2 = 3
[-1, 2, 4], sum = -1 + 2 + 4 = 5
[-1, 2, 4], sum = -1 + 2 + 4 = 5
[2, 2, 4], sum = 2 + 2 + 4 = 8
Triplet [-1, 2, 2], [-1, 2, 4], and [-1, 2, 4] have sum closest to target, so return the maximum one, that is 5.

Following are the approaches to implement the 3 sum closest: To solve this, we will follow these steps:

Implementation of 3 Sum Closest Using Two Pointer Approach

The two-pointer technique is a common approach used to solve problems involving arrays or linked lists. It works by using two pointers that move through the data structure to efficiently find a solution.

In this problem, we will use the two-pointer approach after sorting the array. This helps in reducing the time complexity compared to using three nested loops.

Example

In this example, we solve the 3 closest sum problem using the two-pointer approach in C++:

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
   int threeSumClosest(vector<int>& nums, int target) {
      sort(nums.begin(), nums.end());
      int ans = 0;
      int diff = INT_MAX;
      int n = nums.size();
      for(int i = 0; i < n; i++){
         int left = i + 1;
         int right = n - 1;
         while(left < right){
            int temp = nums[left] + nums[right] + nums[i];
            if(abs(target - temp) < diff){
               ans = temp;
               diff = abs(target - temp);
            }
            if(temp == target)return temp;
            else if(temp > target) right--;
            else left++;
         }
      }
      return ans;
   }
};
int main(){
   Solution ob;
   vector<int> v = {-1,2,1,-4};
   cout<<"3 closest sum is: "<<ob.threeSumClosest(v, 1);
}

Following is the output:

3 closest sum is: 2

The above approach takes O(n^2) time and O(1) space complexity.

Implementation of 3 Sum Closest Using Naive Approach

The naive approach uses a nested loop to traverse all items, exploring all subsets of size 3 and tracking the difference between the target and the sum of the subsets. Then return the sum that comes closest to the target.

Example

In this example, we solve the 3 closest sum problem using naive approach in C++:

#include <iostream>
#include <limits.h>
#include <vector>
using namespace std;
int closest_three_Sum(vector < int > & arr, int target) {
   int n = arr.size();
   int min_diff = INT_MAX;
   int res = 0;
   // Generating all possible triplets
   for (int i = 0; i < n - 2; i++) {
      for (int j = i + 1; j < n - 1; j++) {
         for (int k = j + 1; k < n; k++) {
            int curr_sum = arr[i] + arr[j] + arr[k];
            int curr_diff = abs(curr_sum - target);
            if (curr_diff < min_diff) {
               res = curr_sum;
               min_diff = curr_diff;
            }
            // Take the maximum sum
            // if multiple closest are there
            else if (curr_diff == min_diff) {
               res = max(res, curr_sum);
            }
         }
      }
   }
   return res;
}
int main() {
   vector < int > arr = {-1, 2, 2, 4};
   int target = 4;
   cout << "3 closest sum is: " << closest_three_Sum(arr, target);
   return 0;
}

Following is the output:

3 closest sum is: 5

The above approach takes O(n^3) time and O(1) space complexity.

Conclusion

In this article, we learned so far what the 3Sum Closest problem is and how to solve it using the naive and two-pointer approaches. The two-pointer approach is better than the naive approach.

Updated on: 2025-07-29T15:18:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements