713. Subarray Product Less Than K
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find the number of contiguous subarrays where the product of all elements is strictly less than a given integer k. The input array nums consists of positive integers.
Solution
Subarray Product Less Than K in Python
class Solution:
def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
# If k is less than or equal to 1, no subarray with positive integers can have a product less than k.
if k <= 1:
return 0
count = 0
product = 1
left = 0
# Use a sliding window approach with 'right' as the right boundary.
for right, val in enumerate(nums):
# Expand the window by including the element at 'right'.
product *= val
# Shrink the window from the left while the product is not less than k.
while product >= k:
# Remove the element at 'left' from the product.
product /= nums[left]
# Move the left boundary of the window to the right.
left += 1
# For a valid window [left, right], all subarrays ending at 'right'
# are also valid. These are [right], [right-1, right], ..., [left, right].
# The number of such subarrays is (right - left + 1).
count += (right - left + 1)
return count
Subarray Product Less Than K in Java
class Solution {
public int numSubarrayProductLessThanK(int[] nums, int k) {
// If k is less than or equal to 1, no subarray with positive integers can have a product less than k.
if (k <= 1) {
return 0;
}
int count = 0;
long product = 1; // Use long to avoid potential overflow.
int left = 0;
// Use a sliding window approach with 'right' as the right boundary.
for (int right = 0; right < nums.length; right++) {
// Expand the window by including the element at 'right'.
product *= nums[right];
// Shrink the window from the left while the product is not less than k.
while (product >= k) {
// Remove the element at 'left' from the product.
product /= nums[left];
// Move the left boundary of the window to the right.
left++;
}
// For a valid window [left, right], all subarrays ending at 'right'
// are also valid. These are [right], [right-1, right], ..., [left, right].
// The number of such subarrays is (right - left + 1).
count += (right - left + 1);
}
return count;
}
}
Subarray Product Less Than K in C++
class Solution {
public:
int numSubarrayProductLessThanK(vector<int>& nums, int k) {
// If k is less than or equal to 1, no subarray with positive integers can have a product less than k.
if (k <= 1) {
return 0;
}
int count = 0;
long long product = 1; // Use long long to avoid potential overflow, though int might suffice with given constraints.
int left = 0;
// Use a sliding window approach with 'right' as the right boundary.
for (int right = 0; right < nums.size(); ++right) {
// Expand the window by including the element at 'right'.
product *= nums[right];
// Shrink the window from the left while the product is not less than k.
while (product >= k) {
// Remove the element at 'left' from the product.
product /= nums[left];
// Move the left boundary of the window to the right.
left++;
}
// For a valid window [left, right], all subarrays ending at 'right'
// are also valid. These are [right], [right-1, right], ..., [left, right].
// The number of such subarrays is (right - left + 1).
count += (right - left + 1);
}
return count;
}
};
Subarray Product Less Than K in TypeScript
function numSubarrayProductLessThanK(nums: number[], k: number): number {
// If k is less than or equal to 1, no subarray with positive integers can have a product less than k.
if (k <= 1) {
return 0;
}
let count = 0;
let product = 1;
let left = 0;
// Use a sliding window approach with 'right' as the right boundary.
for (let right = 0; right < nums.length; right++) {
// Expand the window by including the element at 'right'.
product *= nums[right];
// Shrink the window from the left while the product is not less than k.
while (product >= k) {
// Remove the element at 'left' from the product.
product /= nums[left];
// Move the left boundary of the window to the right.
left++;
}
// For a valid window [left, right], all subarrays ending at 'right'
// are also valid. These are [right], [right-1, right], ..., [left, right].
// The number of such subarrays is (right - left + 1).
count += (right - left + 1);
}
return count;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.