#include <algorithm>
#include <cmath>
class compar {
bool operator()( int i, int j ) {
return i < j;
}
};
class Solution {
public:
compar comparobject;
int threeSumClosest(vector<int> &num, int target) {
sort(num.begin(), num.end());
int diff = INT_MAX;
int ans = INT_MAX;
for( int k = 0 ; k < num.size() ; k++ ) {
int i = k+1, j = num.size()-1;
while ( i < j ) {
int localsum = num[i] + num[j] + num[k];
int localdiff = abs(target - localsum);
if( localdiff < diff ) {
diff = localdiff;
ans = localsum;
}
if( localsum < target ) {
i++;
}
else if ( localsum > target ) {
j--;
}
else {
break;
}
}
}
return ans;
}
};
System Design for Beginners
A masterclass that helps early engineers and product managers become great at designing scalable systems.
132+ learners
Details →System Design Masterclass
A masterclass that helps you become great at designing scalable, fault-tolerant, and highly available systems.
1000+ learners
Details →Redis Internals
Learn internals of Redis by re-implementing some of the core features in Golang.
98+ learners
Details →