Count contiguous subarrays whose product is strictly less than k

Quick Overview

Count the contiguous subarrays of a positive integer array whose product is strictly less than a threshold k. It tests linear-time counting over subarrays, careful handling of a strict inequality, and the edge cases where k is 0 or 1.

Count contiguous subarrays whose product is strictly less than k

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an array of positive 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 def num_subarray_product_less_than_k(nums: list[int], k: int) -> int: ``` ### Rules - A subarray is a non-empty run of consecutive elements `nums[i..j]` with `i <= j`. - Subarrays are counted by position. Two subarrays with the same values at different indices count separately. - The comparison is strict: a subarray whose product equals `k` does not count. ### Constraints - `1 <= len(nums) <= 3 * 10^4` - `1 <= nums[i] <= 1000` - `0 <= k <= 10^6` ### Examples ```text Input: nums = [10, 5, 2, 6], k = 100 Output: 8 ``` The 8 subarrays with product less than 100 are [10], [5], [2], [6], [10, 5], [5, 2], [2, 6] and [5, 2, 6]. [10, 5, 2] has product exactly 100, which is not strictly less than 100, and the whole array has product 600. ```text Input: nums = [1, 2, 3], k = 0 Output: 0 ``` Every element is at least 1, so every product is positive and none is strictly less than 0. ```text Input: nums = [1, 1, 1], k = 1 Output: 0 ``` All six subarrays have product 1. That equals `k`, so none of them counts.

Overview: Count the contiguous subarrays of a positive integer array whose product is strictly less than a threshold k. It tests linear-time counting over subarrays, careful handling of a strict inequality, and the edge cases where k is 0 or 1.

General logo
General
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an array of positive 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

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

Rules

  • A subarray is a non-empty run of consecutive elements nums[i..j] with i <= j .
  • Subarrays are counted by position. Two subarrays with the same values at different indices count separately.
  • The comparison is strict: a subarray whose product equals k does not count.

Constraints

  • 1 <= len(nums) <= 3 * 10^4
  • 1 <= nums[i] <= 1000
  • 0 <= k <= 10^6

Examples

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

The 8 subarrays with product less than 100 are [10], [5], [2], [6], [10, 5], [5, 2], [2, 6] and [5, 2, 6]. [10, 5, 2] has product exactly 100, which is not strictly less than 100, and the whole array has product 600.

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

Every element is at least 1, so every product is positive and none is strictly less than 0.

Input: nums = [1, 1, 1], k = 1
Output: 0

All six subarrays have product 1. That equals k, so none of them counts.