Problem 320
Subarrays with Product Less Than K
Medium · 20 pts · Array, Sliding Window, Two Pointers
Problem
All elements of the array are positive. Count the contiguous subarrays whose product is strictly less than K. Because every element is at least 1, extending a subarray never decreases its product, which makes a sliding window possible: for each right end, keep the smallest left end whose window product is still below K.
Input
The first line contains N and K. The second line contains N positive integers.
Output
Print the number of subarrays with product less than K.
Constraints
1 <= N <= 30000; 1 <= A[i] <= 1000; 0 <= K <= 1000000
Example input
4 100 10 5 2 6
Example output
8
Explanation
With K = 100 in 10 5 2 6, the qualifying subarrays are [10], [5], [2], [6], [10 5], [5 2], [2 6] and [5 2 6], so there are 8. The subarray [10 5 2] has product 100, which is not less than 100.