Skip to main content

Subarray Product Less Than K - Solution & Explanation

MediumArrayBinary SearchSliding WindowPrefix Sum20 min readAsked at: Amazon, Microsoft, Apple +17
Practice this problem

Problem Statement

Given an array of integers nums and an integer k, return the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than k.

 

Example 1:

Input: nums = [10,5,2,6], k = 100
Output: 8
Explanation: The 8 subarrays that have product less than 100 are:
[10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6]
Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.

Example 2:

Input: nums = [1,2,3], k = 0
Output: 0

 

Constraints:

  • 1 <= nums.length <= 3 * 104
  • 1 <= nums[i] <= 1000
  • 0 <= k <= 106

Approach Overview

Problem Overview: Given an integer array nums and an integer k, count the number of contiguous subarrays whose product is strictly less than k. The challenge is avoiding the obvious quadratic enumeration while maintaining the product constraint efficiently.

Approach 1: Brute Force (O(n²) time, O(1) space)

The straightforward method enumerates every possible subarray starting at index i. For each start position, keep a running product while extending the subarray to the right. Each time the product remains less than k, increment the count. Once the product becomes greater than or equal to k, further expansion from that start index will only increase the product, so you can stop early for that starting point. This approach is easy to reason about and demonstrates understanding of contiguous subarray enumeration in an array. However, the worst‑case runtime still reaches O(n²), which becomes slow for large inputs.

Approach 2: Sliding Window / Two Pointers (O(n) time, O(1) space)

The optimal solution uses the sliding window technique. Maintain two pointers left and right that define a window and track the running product of elements inside it. Expand the window by moving right and multiplying the new value into the product. If the product becomes greater than or equal to k, shrink the window by moving left forward and dividing its value from the product until the constraint is satisfied again.

The key observation: when the product of the current window is less than k, every subarray ending at right and starting anywhere between left and right is valid. That means you can add right - left + 1 to the answer in constant time instead of enumerating each subarray individually. Each element enters and leaves the window at most once, giving a linear O(n) runtime.

This works because all numbers are positive, so expanding the window always increases the product and shrinking always decreases it. If negative or zero values were allowed, the monotonic behavior would break and a different strategy such as prefix techniques or specialized prefix sum transformations would be required.

Recommended for interviews: Interviewers expect the sliding window approach. Starting with brute force shows you understand the problem and the contiguous constraint. Transitioning to the two‑pointer optimization demonstrates pattern recognition and the ability to reduce quadratic scans to linear time.

Approach 1: Sliding Window Approach

The sliding window approach allows us to efficiently find subarrays where the product is less than k. By maintaining a window (two pointers), we can dynamically adjust the size of the current subarray based on the product of its elements. As the window size grows, the product is updated; if it exceeds k, we reduce the window from the left until the product is again less than k. This method minimizes redundant calculations.

The solution leverages a sliding window (or two-pointer) technique. The product of elements within the window is maintained, and pointers left and right are used to denote the current subarray. If the product exceeds k, the left pointer is moved to shrink the window until the product is less than k. The count of subarrays ending at each index is added to the total count.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n) - Each element is processed at most twice.
Space Complexity: O(1) - Only a constant amount of extra space is used.

Try this approach in the editor →

Approach 2: Brute Force Approach

The brute force approach involves checking every possible subarray within nums and calculating their products. Each subarray is counted if its product is less than k. Though less efficient, it guarantees correctness by examining all potential subarrays.

This C function explores all subarrays starting from each index and computes their products. It stops extending a subarray when its product reaches or exceeds k, thereby optimizing to some degree compared to recalculating products from scratch for each subarray.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n^2) - Considers all possible subarrays.
Space Complexity: O(1) - Constant extra space used.

Try this approach in the editor →

Approach 3: Two Pointers

We can use two pointers to maintain a sliding window, where the product of all elements in the window is less than k.

Define two pointers l and r pointing to the left and right boundaries of the sliding window, initially l = r = 0. We use a variable p to record the product of all elements in the window, initially p = 1.

Each time, we move r one step to the right, adding the element x pointed to by r to the window, and update p = p times x. Then, if p geq k, we move l one step to the right in a loop and update p = p \div nums[l] until p < k or l \gt r. Thus, the number of contiguous subarrays ending at r with a product less than k is r - l + 1. We then add this number to the answer and continue moving r until r reaches the end of the array.

The time complexity is O(n), where n is the length of the array. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

Kotlin

C#

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Sliding Window Approach

Time Complexity: O(n) - Each element is processed at most twice.
Space Complexity: O(1) - Only a constant amount of extra space is used.

Brute Force Approach

Time Complexity: O(n^2) - Considers all possible subarrays.
Space Complexity: O(1) - Constant extra space used.

Two Pointers—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force EnumerationO(n²)O(1)Useful for understanding the problem or when constraints are small
Sliding Window (Two Pointers)O(n)O(1)Optimal solution for positive integers; standard interview approach

Video Solution

LeetCode 713. Subarray Product Less Than K (Algorithm Explained) • Nick White • 37,378 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Subarray Product Less Than K easy or hard?
Subarray Product Less Than K is classified as a Medium problem on LeetCode. The difficulty comes from recognizing that enumerating all subarrays is unnecessary and that a sliding window can count valid ranges in linear time.
Subarray Product Less Than K Python/Java solution
Most implementations use the same sliding window logic across languages. Maintain two pointers, update the product while expanding the window, shrink when the product exceeds the limit, and accumulate the count using right - left + 1. The algorithm works identically in Python, Java, C++, C#, and JavaScript.
How to solve Subarray Product Less Than K in O(n)?
Use a sliding window with two pointers. Expand the window by moving the right pointer and multiply the new value into the running product. If the product becomes >= k, move the left pointer forward and divide values until the product is valid again. Add right - left + 1 to the result at each step.
What is the best approach for Subarray Product Less Than K?
The sliding window (two pointers) approach is the best solution. Maintain a running product while expanding the right pointer and shrink the window when the product becomes >= k. Each step counts all valid subarrays ending at the current index, resulting in O(n) time and O(1) space.
Is Subarray Product Less Than K asked at Google/Amazon/Meta?
Subarray product and sliding window problems appear frequently in interviews at companies like Amazon, Google, and Meta. The question tests two‑pointer reasoning, window maintenance, and recognizing when a monotonic property allows linear scanning.
What data structure is used in Subarray Product Less Than K?
The problem primarily uses the sliding window technique on an array with two pointers and a running product variable. No additional data structures are required beyond constant extra space.
What is the time complexity of Subarray Product Less Than K?
The optimal sliding window solution runs in O(n) time because each element is added to and removed from the window at most once. The brute force approach takes O(n^2) time since it checks every possible subarray.

Ready to solve this problem?

Practice Subarray Product Less Than K with our built-in code editor and test cases.

Practice on FleetCode