Subarray Product Less Than K
Medium
Topics
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 Explanation: The qualifying subarrays are [10], [5], [2], [6], [10,5], [5,2], [2,6] and [5,2,6].
Example 2
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
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
- Hold a running product over a window
[l, r], starting empty with product 1. - Extend
rby one and multiply its value in. - While the product is at least
kand the window is non-empty, divide outnums[l]and advancel. - Add
r - l + 1to the answer, which counts every qualifying subarray ending atrat 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 <= 1has 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 totalimport java.util.*;
class Solution {
public int numSubarrayProductLessThanK(int[] nums, int target) {
if (target <= 1) return 0;
long product = 1;
int total = 0, left = 0;
for (int right = 0; right < nums.length; right++) {
product *= nums[right];
while (product >= target) {
product /= nums[left];
left++;
}
total += right - left + 1;
}
return total;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int numSubarrayProductLessThanK(vector<int>& nums, int target) {
if (target <= 1) return 0;
long long product = 1;
int total = 0, left = 0;
for (int right = 0; right < (int)nums.size(); right++) {
product *= nums[right];
while (product >= target) {
product /= nums[left];
left++;
}
total += right - left + 1;
}
return total;
}
};/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/ var numSubarrayProductLessThanK = function(nums, target) {
if (target <= 1) return 0;
let total = 0, product = 1, left = 0;
for (let right = 0; right < nums.length; right++) {
product *= nums[right];
while (product >= target) {
product /= nums[left];
left++;
}
total += right - left + 1;
}
return total;
};