Ctrl K

A Strong Enough Stretch

Hard
A sensor logged a series of positive readings readings, and you are given a threshold bar. A stretch of length k (consecutive readings) is called strong when **every** reading in it is strictly greater than bar / k. The longer the stretch, the lower the bar for its elements. Return the length of a strong stretch, or -1 if there is none. The input guarantees that all strong stretches, if any, share the same length, so the answer is unique.
Example 1
readings=[2, 7, 7, 1], bar=12 → 2

The stretch [7, 7] of length 2 works: 7 > 12 / 2 = 6. No single reading exceeds 12, and every stretch of length 3 or 4 contains a 2 or a 1, which is too small.

Example 2
readings=[3, 3, 3], bar=9 → -1

Length k needs every reading above 9 / k, that is 3·k > 9. Even k = 3 gives exactly 9, not more, so the answer is -1.

Example 3
readings=[5, 1, 5], bar=4 → 1

A single reading of 5 is already above 4 / 1, while any longer stretch picks up the 1.

Constraints
  • 1 ≤ len(readings) ≤ 10⁵
  • 1 ≤ readings[i] ≤ 10⁹
  • 1 ≤ bar ≤ 10⁹
  • All strong stretches have the same length
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLong Leaps Across a Board HardWater Between Walls HardBiggest Rectangle Under the Skyline HardBiggest Solid Panel Block HardLargest Number From Two Rows Hard
loading Python…
Input
readings=[2, 7, 7, 1], bar=12
Expected
2

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