# 713. Subarray Product Less Than K
You are given an array of positive integers nums.
Count and print the number of (contiguous) subarrays where the product of all the elements in the subarray is less than k.
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.
# Solution
Approach 1: sliding window/two pointers.
# Code (Python)
Approach 1:
class Solution:
def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
# sliding window (two pointers)
count = 0
l = 0
product = 1
for r in range(len(nums)):
product *= nums[r]
while product >= k and l <= r: # l <= r guarantees l doesn't go too far and can be r + 1
product = product // nums[l]
l += 1
count += r - l + 1
return count
# Code (C++)
Approach 1:
Approach 2: