 All Problems
Subarray Product Less Than K
medium
sliding window
arrays
amazon
google
facebook

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.

Example 1:

Input: 10 5 2 6
k: 100
Output: 8
Explanation: [10],[5],[2],[6],[10,5],[5,2],[2,6],[5,2,6] all have product < 100.

Example 2:

Input: 1 2 3
k: 0
Output: 0

Constraints:

  • 1 ≤ nums.length ≤ 3 × 10⁴
  • 1 ≤ nums[i] ≤ 1000
  • 0 ≤ k ≤ 10⁶

Input format: First line: space-separated array. Second line: k.

Output format: Count of subarrays.

Run to check your code against the sample cases, or submit to run every case