Subarray Product Less Than K

Given an array of positive integers `nums` and an integer `k`, return the number of contiguous subarrays whose product is strictly less than `k`.

30:00

Subarray Product Less Than K
medium
Free written solution
Topics
Companies

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

Example 1:
Input: {"nums":[10,5,2,6],"k":100}
Output: 8
Constraints:
  • 1≤nums.length≤3×1041 \leq \text{nums.length} \leq 3 \times 10^4

  • 1≤nums[i]≤10001 \leq \text{nums}[i] \leq 1000

  • 0≤k≤1060 \leq k \leq 10^6

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[10,5,2,6],"k":100}

k = 100 is above 1, so single small numbers can already qualify. Start the window.

nums

10

0

5

1

2

2

6

3

window product (log scale)

prod = 1 < k = 100

k

window [0, -1] size 0

count = 0

Variables
VariableValue
k100
left0
right-1
prod1
count0
DepthFunction Call
Stack empty
0/11