Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 * 1041 <= nums[i] <= 10000 <= k <= 106Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search · Sliding Window
[10,5,2,6] 100
[1,2,3] 0
maximum-product-subarray)maximum-size-subarray-sum-equals-k)subarray-sum-equals-k)two-sum-less-than-k)number-of-smooth-descent-periods-of-a-stock)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #713: Subarray Product Less Than K
class Solution {
public int numSubarrayProductLessThanK(int[] nums, int k) {
int ans = 0, l = 0;
int p = 1;
for (int r = 0; r < nums.length; ++r) {
p *= nums[r];
while (l <= r && p >= k) {
p /= nums[l++];
}
ans += r - l + 1;
}
return ans;
}
}
// Accepted solution for LeetCode #713: Subarray Product Less Than K
func numSubarrayProductLessThanK(nums []int, k int) (ans int) {
l, p := 0, 1
for r, x := range nums {
p *= x
for l <= r && p >= k {
p /= nums[l]
l++
}
ans += r - l + 1
}
return
}
# Accepted solution for LeetCode #713: Subarray Product Less Than K
class Solution:
def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
ans = l = 0
p = 1
for r, x in enumerate(nums):
p *= x
while l <= r and p >= k:
p //= nums[l]
l += 1
ans += r - l + 1
return ans
// Accepted solution for LeetCode #713: Subarray Product Less Than K
impl Solution {
pub fn num_subarray_product_less_than_k(nums: Vec<i32>, k: i32) -> i32 {
let mut ans = 0;
let mut l = 0;
let mut p = 1;
for (r, &x) in nums.iter().enumerate() {
p *= x;
while l <= r && p >= k {
p /= nums[l];
l += 1;
}
ans += (r - l + 1) as i32;
}
ans
}
}
// Accepted solution for LeetCode #713: Subarray Product Less Than K
function numSubarrayProductLessThanK(nums: number[], k: number): number {
const n = nums.length;
let [ans, l, p] = [0, 0, 1];
for (let r = 0; r < n; ++r) {
p *= nums[r];
while (l <= r && p >= k) {
p /= nums[l++];
}
ans += r - l + 1;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.