Ctrl K

Windows With a Small Product

Medium
You are given an array of positive integers values and a threshold limit. A window is a non-empty run of consecutive elements of the array. Count the windows whose product of all elements is **strictly less** than limit. Windows with the same values at different positions count separately. Checking every window takes O(n²); try to do it in O(n).
Example 1
values=[3, 4, 2, 10], limit=30 → 8

The windows [3], [4], [2], [10], [3, 4] (12), [4, 2] (8), [2, 10] (20) and [3, 4, 2] (24) qualify, 8 in total. [4, 2, 10] has product 80, which is too much.

Example 2
values=[7, 7], limit=7 → 0

Even a single 7 is not below the limit 7, so the answer is 0.

Example 3
values=[1, 1, 1], limit=2 → 6

Every window has product 1 < 2, and an array of length 3 has exactly 6 windows.

Constraints
  • 1 ≤ len(values) ≤ 3·10⁴
  • 1 ≤ values[i] ≤ 1000
  • 0 ≤ limit ≤ 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsShortest Stretch to the Goal MediumLongest Run of Ones With k Fixes MediumTrim the Ends to a Target MediumEven Out With Increments MediumRug Over Painted Stretches Medium
loading Python…
Input
values=[3, 4, 2, 10], limit=30
Expected
8

Run checks the first 3 tests in your browser. Submit checks all 123 tests on the server, hidden ones included.