Ctrl K

Cut the Middle to Restore Order

Medium
A sensor logged a series of values readings. You want the series to never decrease, and you are allowed exactly one operation: remove **one contiguous block** (it may be empty). The remaining values are joined together in their original order. Return the smallest block length whose removal leaves a non-decreasing series (each value no smaller than the one before it).
Example 1
readings=[2, 4, 9, 1, 3, 5, 6] → 3

Cut out [9, 1, 3] and [2, 4, 5, 6] remains, which never decreases. Removing only two values is not enough.

Example 2
readings=[7, 6, 5] → 2

The values only go down, so only one of them can stay: two must go.

Example 3
readings=[1, 1, 3, 8] → 0

The series already never decreases, so nothing has to be cut.

Constraints
  • 1 ≤ len(readings) ≤ 10⁵
  • 0 ≤ readings[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWidest Fence Span Around the Marked Board HardLow, Peak, Middle MediumFix the Middle of the Line MediumLongest Uphill Stretch MediumWater Between Walls Hard
loading Python…
Input
readings=[2, 4, 9, 1, 3, 5, 6]
Expected
3

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