106. Subarray Product Less Than K

MediumArrayArray

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

Input: Positive integer array nums and integer k.

Output: Integer — count of subarrays with product < k.

Examples

Example 1
Input: [10,5,2,6],100
Output: 8
Explanation: Subarrays with product<100: [10],[5],[2],[6],[10,5],[5,2],[2,6],[5,2,6]. Count=8.
Example 2
Input: [1,2,3],0
Output: 0
Explanation: k=0 so no product<0.
Example 3
Input: [1,1,1,1],2
Output: 10
Explanation: All subarrays of all 1s have product 1<2.

Constraints

Asked by

AppleAmazonOracleBloombergGoogleMeta
Solve this problem in the editor →