Subarray Product Less Than K
Given an array of positive integers nums and an integer k, return the number of contiguous subarrays whose product of all elements is strictly less than k.
Open official problem prompt ↗Count how many contiguous slices have a product strictly below the threshold k.
Imagine filling a shopping cart from left to right where each item multiplies your total cost. As soon as the cart is too expensive you remove items from the front until it is affordable again; every affordable cart ending at the current item is a valid purchase to tally.
- Input
- nums = [10, 5, 2, 6], k = 100
- Output
- 8
- Why
- The qualifying subarrays are [10], [5], [2], [6], [10,5], [5,2], [2,6], [5,2,6] — eight in total ([10,5,2] has product 100, not < 100).
1 <= nums.length <= 3 * 10^41 <= nums[i] <= 10000 <= k <= 10^6