Count of Subarrays with Product Less Than K
Count of Subarrays with Product Less Than K is a medium arrays & hashing problem solved with the sliding window · variable pattern.
The best approach, optimal (variable sliding window), runs in O(n) time and O(1) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from brute force up.
Problem
Given an array of positive integers nums and an integer k, count the contiguous subarrays whose product is strictly less than k.
Examples
Example 1
- Input
nums = [4, 2, 6, 1], k = 20- Output
8- Why
- Singles [4] [2] [6] [1], pairs [4,2] [2,6] [6,1], and [2,6,1] with product 12. [4,2,6] is 48, too big.
Example 2
- Input
nums = [1, 2, 3], k = 0- Output
0
Constraints
1 <= nums.length <= 3 * 10^41 <= nums[i] <= 10000 <= k <= 10^6
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (2 examples, or your own). Submit also runs 11 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Brute force | O(n²) | O(1) |
| Optimal (variable sliding window) | O(n) | O(1) |
1Brute force
O(n²)O(1)For each start, extend while the product stays below k, counting each subarray.
- For each i: p = 1; for j from i: p *= nums[j]; stop when p >= k, else count++.
class Solution {
public int numSubarrayProductLessThanK(int[] nums, int k) {
int count = 0;
for (int i = 0; i < nums.length; i++) {
long p = 1;
for (int j = i; j < nums.length; j++) {
p *= nums[j];
if (p >= k) break;
count++;
}
}
return count;
}
}class Solution:
def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
count = 0
for i in range(len(nums)):
p = 1
for j in range(i, len(nums)):
p *= nums[j]
if p >= k:
break
count += 1
return countclass Solution {
public:
int numSubarrayProductLessThanK(vector<int>& nums, int k) {
int count = 0;
for (int i = 0; i < (int) nums.size(); i++) {
long long p = 1;
for (int j = i; j < (int) nums.size(); j++) {
p *= nums[j];
if (p >= k) break;
count++;
}
}
return count;
}
};function numSubarrayProductLessThanK(nums, k) {
let count = 0;
for (let i = 0; i < nums.length; i++) {
let p = 1;
for (let j = i; j < nums.length; j++) {
p *= nums[j];
if (p >= k) break;
count++;
}
}
return count;
}2Optimal (variable sliding window)
O(n)O(1)Grow the window on the right and multiply in nums[r]. While the product is k or more, divide out nums[l] and move l. Every subarray that ends at r and starts anywhere in [l, r] is valid, which adds r - l + 1.
- If k <= 1 return 0.
- For each r: prod *= nums[r]; while prod >= k: prod /= nums[l++].
- count += r - l + 1.
class Solution {
public int numSubarrayProductLessThanK(int[] nums, int k) {
if (k <= 1) return 0;
int l = 0, count = 0;
long prod = 1;
for (int r = 0; r < nums.length; r++) {
prod *= nums[r];
while (prod >= k) prod /= nums[l++];
count += r - l + 1;
}
return count;
}
}class Solution:
def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
if k <= 1:
return 0
l, count = 0, 0
prod = 1
for r in range(len(nums)):
prod *= nums[r]
while prod >= k:
prod //= nums[l]
l += 1
count += r - l + 1
return countclass Solution {
public:
int numSubarrayProductLessThanK(vector<int>& nums, int k) {
if (k <= 1) return 0;
int l = 0, count = 0;
long long prod = 1;
for (int r = 0; r < (int) nums.size(); r++) {
prod *= nums[r];
while (prod >= k) prod /= nums[l++];
count += r - l + 1;
}
return count;
}
};function numSubarrayProductLessThanK(nums, k) {
if (k <= 1) return 0;
let l = 0, count = 0;
let prod = 1;
for (let r = 0; r < nums.length; r++) {
prod *= nums[r];
while (prod >= k) prod /= nums[l++];
count += r - l + 1;
}
return count;
}Edge cases to test
- k <= 1 (no product of positive integers is below 1)
- Single elements already >= k
Hints
Hint 1
For a window [l, r] whose product is below k, how many valid subarrays end at r?
FAQ
What is the best time complexity for Count of Subarrays with Product Less Than K?
Optimal (variable sliding window) runs in O(n) time and O(1) extra space.
Which pattern does Count of Subarrays with Product Less Than K use?
It is a arrays & hashing problem that uses the sliding window · variable pattern. Other problems with the same pattern: Minimum Size Subarray Sum, Longest Repeating Character Replacement, Longest Substring Without Repeating Characters.
Is there a brute force solution for Count of Subarrays with Product Less Than K?
Yes. Brute force takes O(n²) time and O(1) space. For each start, extend while the product stays below k, counting each subarray.
Which edge cases should I test for Count of Subarrays with Product Less Than K?
k <= 1 (no product of positive integers is below 1); Single elements already = k.