Advertisement
nikunjsoni

259

Jul 4th, 2021
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  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. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement