Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int Solution::threeSumClosest(vector<int> &A, int B) {
- long long i=0;
- long long a;
- long long b=INT_MIN;
- sort(A.begin(),A.end());
- for(int i=0;i<A.size()-2;i++)
- {
- a=A[i];
- long long temp=B-a;
- long long left=i+1,right=A.size()-1,ans=INT_MIN;
- while(left<right)
- {
- long long sum=A[left]+A[right];
- ans=(temp-ans<temp-sum)?ans:sum;
- if(sum<temp)
- {
- left++;
- }
- else if(sum>temp)
- {
- right--;
- }
- else
- {
- break;
- }
- yr
- }
- a+=ans;
- if(abs(B-a)<abs(B-b))
- b=a;
- //cout<<i<<b<<endl;
- }
- //cout<<b;
- return b;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement