Ctrl K

Two Edits to Tame the Spread

Medium
You are given an array of readings readings. Two quantities are defined for it: - the **low** value: the smallest absolute difference between two elements at different indices; - the **high** value: the largest such difference. The **score** of the array is low plus high. You may replace exactly two elements with any integers you like. Return the smallest score you can achieve.
Example 1
readings=[3, 20, 9, 11, 1] → 8

Change 1 and 20 into 9 to get [3, 9, 9, 11, 9]. The smallest gap is 0, the largest is 11 − 3 = 8, so the score is 8.

Example 2
readings=[7, 50, 2] → 0

Turn 50 and 2 into 7: all values are equal and the score is 0.

Example 3
readings=[1, 2, 3, 100, 200] → 2

Fixing the two largest values is best: 1, 2 and 3 remain, so the score is 3 − 1 = 2.

Constraints
  • 3 ≤ len(readings) ≤ 10⁵
  • 1 ≤ 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=[3, 20, 9, 11, 1]
Expected
8

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