Z16

Subarray Product Less Than K

Staff / Principal Engineer
Target Time: 40 - 50 Minutes
Type: NUMERIC

Problem Statement & Objective

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

Function Signature (Python)

subarray_product_less_than_k(nums: list[int], k: int) -> int

Input & Output Contract

Output Rule:Count of contiguous subarrays with product < k.

System Constraints

1 <= nums.length <= 3 * 10^4
1 <= nums[i] <= 1000
0 <= k <= 10^6

Example Execution Walkthrough

nums = [10, 5, 2, 6], k = 100
subarrays: [10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6] -> result 8

Example Test Scenarios (3)

Example 1
Input:[[10,5,2,6],100]
Expected Output:8
Example 2: k=0
Input:[[1,2,3],0]
Expected Output:0
Example 3: Birler
Input:[[1,1,1],2]
Expected Output:6
Loading Python Editor...
Input Parameters
[
  [
    10,
    5,
    2,
    6
  ],
  100
]
Expected Output
8