-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClosestSumThree.cpp
42 lines (37 loc) · 1.4 KB
/
ClosestSumThree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<bits/stdc++.h>
using namespace std;
/*
* Given an array, arr of integers, and another number target,
* find three integers in the array such that their sum is closest to the target.
* Return the sum of the three integers.
* Note: If there are multiple solutions, return the maximum one.
* Input: arr[] = [5, 2, 7, 5], target = 13
* Output: 14
* Explanation: There is one triplet with sum 12 and other with sum 14 in the array.
* Triplet elements are 5, 2, 5 and 2, 7, 5 respectively.
* Since abs(13-12) ==abs(13-14) maximum triplet sum will be preferred i.e 14.
*/
int threeSumClosest(vector<int> arr, int target) {
sort(arr.begin(), arr.end());
int closestSum = INT_MAX, prevDiff = INT_MAX, n = arr.size();
for(int i = 0; i < n; i++) {
if(i > 0 && arr[i] == arr[i-1]) continue;
int j = i+1, k = n-1;
while(j < k){
int sum = arr[i] + arr[j] + arr[k];
int currDiff = abs(sum - target);
// Update closestSum if a closer sum is found or if the same difference is found but sum is larger
if(currDiff < prevDiff || (currDiff == prevDiff && sum > closestSum)){
prevDiff = currDiff;
closestSum = sum;
}
if(sum < target)
j++;
else if(sum > target){
k--;
}
else return target;
}
}
return closestSum;
}