Count of Subarrays with Product Less Than K

Medium Arrays & Hashing Sliding Window · Variable Original on LeetCode

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^4
  • 1 <= nums[i] <= 1000
  • 0 <= 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.

00:00
Testcase

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.

ApproachTimeSpace
Brute forceO(n²)O(1)
Optimal (variable sliding window)O(n)O(1)

1Brute force

TimeO(n²)
SpaceO(1)

For each start, extend while the product stays below k, counting each subarray.

  1. 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;
    }
}

2Optimal (variable sliding window)

TimeO(n)
SpaceO(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.

  1. If k <= 1 return 0.
  2. For each r: prod *= nums[r]; while prod >= k: prod /= nums[l++].
  3. 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;
    }
}

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.