Given an array of positive 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.
Function Signature
def num_subarray_product_less_than_k(nums: list[int], k: int) -> int:
Rules
-
A subarray is a non-empty run of consecutive elements
nums[i..j]
with
i <= j
.
-
Subarrays are counted by position. Two subarrays with the same values at different indices count separately.
-
The comparison is strict: a subarray whose product equals
k
does not count.
Constraints
-
1 <= len(nums) <= 3 * 10^4
-
1 <= nums[i] <= 1000
-
0 <= k <= 10^6
Examples
Input: nums = [10, 5, 2, 6], k = 100
Output: 8
The 8 subarrays with product less than 100 are [10], [5], [2], [6], [10, 5], [5, 2], [2, 6] and [5, 2, 6]. [10, 5, 2] has product exactly 100, which is not strictly less than 100, and the whole array has product 600.
Input: nums = [1, 2, 3], k = 0
Output: 0
Every element is at least 1, so every product is positive and none is strictly less than 0.
Input: nums = [1, 1, 1], k = 1
Output: 0
All six subarrays have product 1. That equals k, so none of them counts.