Last active
October 14, 2018 22:45
-
-
Save nimbus98/1d0f0135f0115f7f85922ef0c087b94f to your computer and use it in GitHub Desktop.
IEEE CodeBuddy | Week 3 | Ashwin Joisa & Akash Nair
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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