Given an array of integers nums and an integer k, return the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than k.

&nbsp;
Example 1:

Input: nums = [10,5,2,6], k = 100
Output: 8
Explanation: The 8 subarrays that have product less than 100 are:
[10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6]
Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.


Example 2:

Input: nums = [1,2,3], k = 0
Output: 0


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 3 * 104
	1 &lt;= nums[i] &lt;= 1000
	0 &lt;= k &lt;= 106


class Solution:
    def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
        if k==0 or k==1:
            return 0
        p=1
        ini=0
        fin=0
        n=len(nums)
        c=0
        while fin<n:
            p=p*nums[fin]
            while p>=k :
                p=p//nums[ini]
                ini+=1

            n1=fin-ini+1
            c+=n1
            fin+=1
        return c
class Solution {
    public int numSubarrayProductLessThanK(int[] nums, int k) {
		//if k=0 then ans will always be zero as we have positive integers array only.
        if(k==0)
            return 0;
        
        int length = 0;
        long product = 1;
        int i = 0;
        int j = 0;
        int n = nums.length;
        int ans = 0;
        
        while(j<n){
            product*=nums[j];
			//Add the number to current window if the product is less than k and calculate no of subarrays using length only.
            if(product<k){
                length+=1;
                ans+=length;
            }
            else{
				//Remove element one by one till product becomes less than k.
                while(i<=j && product>=k){
                    product/=nums[i];
                    i++;
                }
				//As we have added only 1 element to the window and this element can make subarray to j-i element along with itself.
                ans+=(j-i)+1;
				//Update the current subarray length.
                length=j-i+1;
            }
            j++;
        }
        
        return ans;
    }
}
class Solution {
public:
    int numSubarrayProductLessThanK(vector<int>& nums, int k) {
        int start = 0;
        long prod = 1;
        int count =0; // count of subarray prod  less than k
        for(int end =0; end< nums.size(); end++){
            prod *= nums[end];
    
           while(prod >= k && start < nums.size()){
               prod = prod/nums[start];// divide product by nums at start pointer t reduce the prod
                start++;//move start pointer because no longer nums at start can give us prod < k
              }
           if(prod < k)
            count += end - start +1;
      }
    return count;
    }
};
var numSubarrayProductLessThanK = function(nums, k) {
    var used = new Array(nums.length).fill(0);
    var l, r, runsum=1;
    let ans=0;
    
    l = 0;
    r = 0;
    while( r < nums.length ) {
        if( r < nums.length && runsum * nums[r] >= k ) {
            if( r != l )
                runsum /= nums[l];
            else
                r++;
            l++;
        } else if( l <= r ) {
            runsum *= nums[r];
            r++;
            ans += (r-l);
        } else break;
    }
    
    return ans;
};
Subarray Product Less Than K