Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int Solution::threeSumClosest(vector<int> &A, int B) {
- int i=0;
- int a;
- long long b=INT_MIN;
- sort(A.begin(),A.end());
- for(int i=0;i<A.size()-2;i++)
- {
- a=A[i];
- int temp=B-a;
- int left=i+1,right=A.size()-1,ans;
- while(left<right)
- {
- int sum=A[left]+A[right];
- if(sum<temp)
- {
- ans=sum;
- left++;
- }
- else if(sum>temp)
- {
- right--;
- }
- else{
- ans=sum;
- break;
- }
- }
- 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