2Brute2Best
🔥0

Subarray Product Less Than K

Medium·LeetCode #713 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You are given an array of positive integers nums and an integer k.

Count how many contiguous subarrays have a product of all their elements that is strictly less than k.

Input

An integer array nums (every element ≥ 1) and an integer k.

Output

The number of subarrays whose product is < k.

Example 1
Input: nums = [10, 5, 2, 6], k = 100
Output: 8

The 8 subarrays are [10], [5], [2], [6], [10, 5], [5, 2], [2, 6] and [5, 2, 6]. [10, 5, 2] has product exactly 100, which is not strictly less than 100.

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

Every product is at least 1, so no product can be less than 0.

Constraints
  • • 1 ≤ nums.length ≤ 3·10^4
  • • 1 ≤ nums[i] ≤ 1000
  • • 0 ≤ k ≤ 10^6
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“number of contiguous subarrays”
→ Counting problem → add something per right end, not a max.
“product strictly less than k”
→ An upper limit. Shrinking the window always helps, so a sliding window can enforce it.
“positive integers (nums[i] ≥ 1)”
→ The product never goes down as the window grows. That monotonic behaviour is what makes the window safe.
03

Approach Complexity Ladder

Brute Force — grow from every startO(N²)O(1)

For each start, multiply to the right until the product reaches k

Optimal — sliding window on the productO(N)O(1)

Keep the longest window ending at right with product < k; add its length

Brute Force Intuition

Fix a start. Extend the end one step at a time, keeping a running product. Count every subarray while the product stays below k. Stop as soon as it reaches k, because the product can only grow from there.

Recognition rule for this sub-pattern:

Count subarrays that satisfy a rule. "At most": for each right end, every start from left to right works, so add (right − left + 1). "At least": once the window is good, every longer one is too, so add left (or n − right).
Remember this · Sliding Window
Trigger
Contiguous subarray / substring + longest, shortest, maximum, count, or "of size K".
Think
Can I keep a valid range and move it, instead of checking every subarray from scratch?
Key question
When is my current window valid?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Sliding Window translate into production architectures and low-level memory mechanics.