Passes All Test Cases


#1
 int n=A.size();
    sort(A.begin(),A.end());
    long  bestSum=1000000000,sum=0;
    for(int i=0;i<n-2;i++){
        int start=i+1,end=n-1;
        while(start<end){
            sum=long(A[i])+A[start]+A[end];
            if(abs(target-sum)<abs(target-bestSum))
                bestSum=sum;
            else if(sum>target)
                end--;
            else
                start++;
        }
    }
    return bestSum;

bestSum =10000000 is used if target is INT_MIN and also to cope with limit of int long is used


#2

Very Helpful Solution
Thanks


#3

You can use bestSum as INT_MAX ,you just have to take by a case when sum matches with target.