Subarray Product Less Than K

Medium

Topics
ArraySliding WindowTwo Pointers

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

100512263product 60
The answer counts contiguous blocks whose values multiply to less than 100; the longest one is braced. Blocks of a single element count too, and overlapping blocks are counted separately — 8 in total here.
Cells are the array values; the small numbers underneath are indices.
Input:  nums = [10,5,2,6], k = 100
Output: 8
Explanation: The qualifying subarrays are [10], [5], [2], [6], [10,5], [5,2], [2,6] and [5,2,6].

Example 2

102132
Every value is at least 1, so no block multiplies to less than 0 and the answer is 0.
Input:  nums = [1,2,3], k = 0
Output: 0
Explanation: A product of positive integers is at least 1, so nothing is below 0.

Example 3

101112product 1
Input:  nums = [1,1,1], k = 2
Output: 6
Explanation: Every one of the six subarrays has product 1.

Constraints

  • 1 <= nums.length <= 3 * 10^4
  • 1 <= nums[i] <= 1000
  • 0 <= k <= 10^6
Editorial — intuition, approach, complexity and pitfalls

Intuition

Every value is at least 1, so extending a subarray can only hold its product steady or raise it. That is what makes a window work: once the product reaches k, the only way back under is to drop elements from the left.

The counting trick is to fix the right end. If the window [l, r] is the longest one ending at r with product below k, then every subarray ending at r and starting at or after l also qualifies — and there are exactly r - l + 1 of them. No need to enumerate them.

Approach

  1. Hold a running product over a window [l, r], starting empty with product 1.
  2. Extend r by one and multiply its value in.
  3. While the product is at least k and the window is non-empty, divide out nums[l] and advance l.
  4. Add r - l + 1 to the answer, which counts every qualifying subarray ending at r at once.

Walkthrough

nums = [10, 5, 2, 6], k = 100

r=0  prod=10         l=0  add 1  (total 1)   [10]
r=1  prod=50         l=0  add 2  (total 3)   [5], [10,5]
r=2  prod=100 >= k -> divide out 10, l=1, prod=10
     add 2  (total 5)   [2], [5,2]
r=3  prod=60         l=1  add 3  (total 8)   [6], [2,6], [5,2,6]

Complexity

  • Time — O(n): each pointer only ever moves forward, so together they traverse the array at most twice.
  • Space — O(1): a product and two indices.

Common Pitfalls

  • k <= 1 has to answer 0 up front: the product of positive integers is never below 1, and without that guard the shrink loop empties the window and divides by nothing.
  • The count is r - l + 1, not 1 — adding one per window counts only the longest subarray at each right end.

Full Solution — all languages

class Solution:
    def numSubarrayProductLessThanK(self, nums, target):
        if target <= 1:
            return 0
        total = 0
        product = 1
        left = 0
        for right, v in enumerate(nums):
            product *= v
            while product >= target:
                product //= nums[left]
                left += 1
            total += right - left + 1
        return total
SensAI
Beta
Tap the mic to stop voice mode