Subarray Product Less Than K

Medium

Description

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

Examples

Input:nums = [10,5,2,6], k = 100
Output:8
Explanation:

8 subarrays have product < 100.

Input:nums = [1,2,3], k = 0
Output:0
Explanation:

Since k = 0 and all numbers are positive, no subarray can have a product strictly less than 0. All possible products will be positive integers ≥ 1.

Input:nums = [1,1,1], k = 2
Output:6
Explanation:

All subarrays have product = 1, which is < 2. The 6 valid subarrays are: [1], [1], [1], [1,1], [1,1], and [1,1,1]. This demonstrates how arrays with small values can produce many valid subarrays.

Constraints

  • 1 ≤ nums.length ≤ 3 * 10^4
  • 1 ≤ nums[i] ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!