16. 3Sum Closest
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int n=nums.size();
std::sort(nums.begin(),nums.end());
int res=nums[0]+nums[1]+nums[n-1];
for(int first=0;first<n-2;first++){
int second=first+1,third=n-1;
while(second<third){
int sum=nums[first]+nums[second]+nums[third];
res=abs(sum-target)<abs(res-target)?sum:res;
if(sum<target)
second++;
else if(sum>target)
third--;
else
return res=sum;
}
}
return res;
}
};