Skip to content

Instantly share code, notes, and snippets.

@nimbus98
Last active October 14, 2018 22:45
Show Gist options
  • Save nimbus98/1d0f0135f0115f7f85922ef0c087b94f to your computer and use it in GitHub Desktop.
Save nimbus98/1d0f0135f0115f7f85922ef0c087b94f to your computer and use it in GitHub Desktop.
IEEE CodeBuddy | Week 3 | Ashwin Joisa & Akash Nair
int Solution::threeSumClosest(vector<int> &A, int B) {
int ans = INT_MIN/2;
int n = A.size();
sort(A.begin(),A.end());
int i, j, k;
for(int i = 0; i < n; i++){
j = i + 1;
k = n - 1;
while(j < k){
if(abs(B - (A[i]+A[j]+A[k])) < abs(B - ans))
ans = A[i] + A[j] + A[k];
if(A[j] + A[k] <= (B - A[i])){
j++;
}
else k--;
}
}
return ans;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment