Whats wrong in this code


#1

int Solution::threeSumClosest(vector &A, int B) {
int n = A.size();
int i = 0, j = 1, k= n-1;
int diff = 0;
int res = 0;
int minD = INT_MAX;
int sum =0;
sort(A.begin(), A.end());
for(i = 0; i < n-2; i++)
{
j = i+1;
k = n-1;
while(j < k)
{
sum = A[i]+A[j]+A[k];
diff = abs(sum-B);
if(diff == 0)
return sum;

        if(diff < minD)
        {
            minD = diff;
            res = sum;
        }
        if(sum < B)
        {
            j++;
        }
        else if(sum > B)
        {
            k--;
        }
    }
}
return sum;

}


#2

In the very last, return res, rather than returning sum again.
So, change the last line from return sum; to return res;