← Visualizers
Sliding Window · variable window, sub-variant F — count LeetCode 713

Subarray Product Less Than K / visualized

A counting window does not look for one best answer — it banks a number at every step and never looks back. r advances and multiplies in; while the product is ≥ k the window is invalid, so l divides out from the left until it is valid again. Then comes the line that is the sub-variant: count += r − l + 1. That is not an off-by-one incantation — it is the exact number of new subarrays that end at r, one for each start in [l..r], drawn as a ladder under the tape. Every shorter suffix of a valid window is itself valid, because all values are ≥ 1 and dropping a factor can only make the product smaller. Two edges the machine has to survive: k ≤ 1 returns 0 outright — no product of positive integers is below 1 — and a single element that already exceeds k drives l past r, leaving the window empty and contributing r − l + 1 = 0.

Execution

idle
nums  ·  teal cells are inside the window
the subarrays this step banks
Press Run to begin.
0 / 0
Speed

Java · running line

inside the window entering (prod ×=) evicted (prod ÷=) subarray counted left behind
O(n) time · O(1) space  ·  each index is multiplied in once and divided out at most once, so the inner while is amortised O(1). Sub-variant F: the answer is not a best window, it is a count accumulated by r - l + 1 at every r. Guard k <= 1 first, or the shrink loop runs the window empty and the arithmetic still reads as if it worked.