-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path16.3SumClosest.cpp
More file actions
26 lines (23 loc) · 911 Bytes
/
16.3SumClosest.cpp
File metadata and controls
26 lines (23 loc) · 911 Bytes
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
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
int n = nums.size(), ans = nums[0] + nums[1] + nums[2];
for (int i = 0; i < n-2; ++i) {
int l = i + 1, r = n - 1;
while (l < r) {
int sum3 = nums[i] + nums[l] + nums[r];
if (abs(ans - target) > abs(sum3 - target)) // Update better ans
ans = sum3;
if (sum3 == target) break;
if (sum3 > target)
--r; // Sum3 is greater than the target, to minimize the difference, we have to decrease our sum3
else
++l; // Sum3 is lesser than the target, to minimize the difference, we have to increase our sum3
}
}
return ans;
}
};