Count Subarrays With Product Less Than K
Solve this Problemnums and an integer k, count the number of contiguous subarrays whose product of all elements is strictly less than k.
Checking every subarray directly works but repeats the same multiplications across overlapping windows. The key trick is that with all-positive values, a sliding windowSliding WindowMaintaining a running result over a contiguous range that grows or shrinks one element at a time, instead of recomputing the result for every range from scratch. whose product stays under k can never help by shrinking further — so once a window [left, right] qualifies, every one of its right - left + 1 suffixes ending at right qualifies too, and they can all be counted in a single step.
Test Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
1 ≤ nums.length ≤ 3 × 10⁴ - ◆
1 ≤ nums[i] ≤ 1000 - ◆
0 ≤ k ≤ 10⁶
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
| 1 | class Solution { |
| 2 | public int countSubarraysWithProductLessThanK(int[] nums, int k) { |
| 3 | if (k <= 1) return 0; |
| 4 | int left = 0, product = 1, count = 0; |
| 5 | for (int right = 0; right < nums.length; right++) { |
| 6 | product *= nums[right]; |
| 7 | while (product >= k) { |
| 8 | product /= nums[left]; |
| 9 | left++; |
| 10 | } |
| 11 | count += right - left + 1; |
| 12 | } |
| 13 | return count; |
| 14 | } |
| 15 | } |
| 16 |
010Start left at 0, product at 1, and count at 0. Grow the window to the right, keeping product below k = 100.
Approach & Solutions
Brute Force
BruteFor every starting index, extend a running product one element at a time and count the subarray as soon as the product stays under k. Stop extending as soon as the product reaches k, since every value is positive and the product can only grow from there. Correct, but every window shares almost all of its work with the one before it.
O(n²)O(1)1class Solution {
2 public int countSubarraysWithProductLessThanK(int[] nums, int k) {
3 if (k <= 1) return 0;
4 int count = 0;
5 for (int i = 0; i < nums.length; i++) {
6 int product = 1;
7 for (int j = i; j < nums.length; j++) {
8 product *= nums[j];
9 if (product >= k) break;
10 count++;
11 }
12 }
13 return count;
14 }
15}Optimal — Sliding Window
OptimalGrow a window [left, right] by multiplying in nums[right]. Whenever the product reaches k, divide out nums[left] and shrink the window from the left until it's under k again. Every subarray ending at right and starting anywhere from left to right also has product less than k — since dropping leading elements can only shrink the product — so all right - left + 1 of them are counted at once, without re-checking each one.
O(n)O(1)1class Solution {
2 public int countSubarraysWithProductLessThanK(int[] nums, int k) {
3 if (k <= 1) return 0;
4 int left = 0, product = 1, count = 0;
5 for (int right = 0; right < nums.length; right++) {
6 product *= nums[right];
7 while (product >= k) {
8 product /= nums[left];
9 left++;
10 }
11 count += right - left + 1;
12 }
13 return count;
14 }
15}