Subarray Product Less Than K (LeetCode 713): Solution and Explanation
Given an array of positive integers and an integer k, count the contiguous subarrays whose product is strictly less than k.
Intuition
Checking every subarray takes O(n^2) time. The positive values give a shortcut: multiplying by a number that is at least 1 never makes a product smaller, so if a subarray's product is too big, every wider subarray around it is too big as well.
Fix the right end. Moving the left end rightward can only shrink the product, so the valid left ends form one block [left, right]. Every start in that block gives a subarray with product below k, which is right - left + 1 subarrays ending at right.
As right moves forward, the smallest valid left never moves back. So one window does the whole job: multiply in the new number, divide out numbers from the left until the product is below k again, then count.
Approach: Sliding window on the product
- If
k <= 1, return 0, because every product of positive integers is at least 1. - Set
prod = 1,left = 0,count = 0. - For each
right, multiplyprodbynums[right]. - While
prod >= k, divideprodbynums[left]and incrementleft. - Add
right - left + 1tocount, the number of subarrays that end atrightand stay below k.
Step-by-step example
nums = [10,5,2,6], k = 100
- right = 0: prod = 10 < 100. The window is [10], add 1. count = 1.
- right = 1: prod = 50 < 100. The window is [10,5], add 2 ([5] and [10,5]). count = 3.
- right = 2: prod = 100, not below 100. Divide out 10, left = 1, prod = 10. The window is [5,2], add 2. count = 5.
- right = 3: prod = 60 < 100. The window is [5,2,6], add 3 ([6], [2,6], [5,2,6]). count = 8.
Code: Python, Java, C++
1class Solution:
2 def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
3 if k <= 1:
4 return 0
5 prod, left, count = 1, 0, 0
6 for right, x in enumerate(nums):
7 prod *= x
8 while prod >= k:
9 prod //= nums[left]
10 left += 1
11 count += right - left + 1
12 return count1class Solution {
2 public int numSubarrayProductLessThanK(int[] nums, int k) {
3 if (k <= 1) {
4 return 0;
5 }
6 int prod = 1, left = 0, count = 0;
7 for (int right = 0; right < nums.length; right++) {
8 prod *= nums[right];
9 while (prod >= k) {
10 prod /= nums[left];
11 left++;
12 }
13 count += right - left + 1;
14 }
15 return count;
16 }
17}1class Solution {
2public:
3 int numSubarrayProductLessThanK(vector<int>& nums, int k) {
4 if (k <= 1) {
5 return 0;
6 }
7 int prod = 1, left = 0, count = 0;
8 for (int right = 0; right < (int)nums.size(); right++) {
9 prod *= nums[right];
10 while (prod >= k) {
11 prod /= nums[left];
12 left++;
13 }
14 count += right - left + 1;
15 }
16 return count;
17 }
18};Time and space complexity
O(n)
right moves n times and left moves at most n times in total, so the loop does O(n) multiplications and divisions.
O(1)
Only the product, two indices and the count are stored.
Other approaches
Brute force with early break (time O(n^2), space O(1))
For each start, multiply forward and stop as soon as the product reaches k. It is easy to get right but quadratic when many long subarrays stay below k, such as an array full of 1s.
Binary search on prefix logs (time O(n log n), space O(n))
Take prefix sums of log(nums[i]). A subarray qualifies when the difference of two prefix logs is below log(k), so binary search finds the first valid start for each right end. It needs care with floating point rounding at the boundary.
Common mistakes
- Forgetting the k <= 1 guard. With k = 1 and nums of 1s, the while loop would push left past right and keep dividing.
- Using <= k instead of >= k in the shrink condition. The product must be strictly less than k.
- Counting only whole windows. Every start inside [left, right] gives a valid subarray, so add the window size, not 1.
- Worrying about overflow: prod is below k before the multiply and nums[i] <= 1000, so prod stays under 10^9 and fits in a 32-bit int.
Frequently asked questions
Why does adding right - left + 1 count each subarray exactly once?
Every subarray has exactly one right end. At that right end, the subarrays counted are [s..right] for every s from left to right, and those are exactly the ones whose product is below k.
Does this work with zeros or negative numbers?
No. The window relies on products growing as the window widens, which only holds for positive values. The problem guarantees nums[i] >= 1.
What is the time complexity of Subarray Product Less Than K?
O(n) with the sliding window, because each index enters and leaves the window at most once. The brute force is O(n^2) and the prefix-log binary search is O(n log n).