Advertisement
nikunjsoni

713

Apr 18th, 2021
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.47 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int numSubarrayProductLessThanK(vector<int>& nums, int k) {
  4.         if(!k) return 0;
  5.         int product, ans, left, right;
  6.         product = 1;
  7.         ans = 0;
  8.         for(left=0, right=0; right<nums.size(); right++){
  9.             product *= nums[right];
  10.             while(left <= right && product >= k){
  11.                 product /= nums[left++];
  12.             }
  13.             ans += (right-left+1);
  14.         }
  15.         return ans;
  16.     }
  17. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement