Ctrl K

Narrow the Range With Three Edits

Medium
A sensor produced a series of readings readings. You may fix at most three of them: each fix replaces one reading with any integer you like. The spread of the series is its largest value minus its smallest value. Return the smallest spread you can achieve.
Example 1
readings=[12, 40, 7, 9, 100, 11] → 3

Edit 7, 40 and 100, say to 10. The values then run from 9 to 12, a spread of 3.

Example 2
readings=[5, -5, 0] → 0

There are only three values, so all of them can be made equal.

Example 3
readings=[1, 2, 3, 4, 5, 6, 7] → 3

However the three edits are spent, four consecutive values survive, so the spread is at least 3.

Constraints
  • 1 ≤ len(readings) ≤ 10⁵
  • -10⁹ ≤ readings[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Number From Pieces MediumStudios for Recording Sessions MediumLow, High, Low MediumStrictly Low, High, Low MediumFewest Meetings to Cancel Medium
loading Python…
Input
readings=[12, 40, 7, 9, 100, 11]
Expected
3

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