nikunjsoni

259

Jul 4th, 2021
101
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2. public:
  3.     int threeSumSmaller(vector<int>& nums, int target) {
  4.         if(nums.size()<3)return 0;
  5.         sort(nums.begin(),nums.end());
  6.         int count=0;
  7.         for(int i=0;i<nums.size()-2;++i){
  8.             if(nums[i]+nums[i+1]+nums[i+2]>=target)break;
  9.             int j=i+1,k=nums.size()-1;
  10.             while(j<k){
  11.                 while(j<k && nums[i]+nums[j]+nums[k]>=target)k--;
  12.                 count+=k-j;
  13.                 j++;
  14.             }
  15.         }
  16.         return count;
  17.     }
  18. };
RAW Paste Data