Leetcode 713 Solution

This article provides solution to leetcode question 713 (subarray-product-less-than-k)

https://leetcode.com/problems/subarray-product-less-than-k

Solution

class Solution {
public:
    int numSubarrayProductLessThanK(vector<int>& nums, int k) {
        int product = 1;
        int l = 0;
        int ans = 0;

        for (int r = 0; r < nums.size(); r++)
        {
            product *= nums[r];

            while (product >= k && l <= r)
                product /= nums[l++];

            ans += r - l + 1;
        }

        return ans;
    }
};