Minimum Difference Between Largest and Smallest Value in Three Moves

Medium Topics Company Tags

Change up to three entries in nums to any values you choose. Return the smallest achievable difference between the largest and smallest entries. You may use fewer than three changes.

Example 1:

Input: nums = [1,5,9,13,17]

Output: 4

Explanation: Change 9, 13, and 17 to 5, leaving [1,5,5,5,5] with range 4. At least two original entries must remain, and any two differ by at least 4.

Example 2:

Input: nums = [8,8,8,8,100]

Output: 0

Explanation: Change 100 to 8 in one move. All entries then equal 8, so the difference is 0.

Example 3:

Input: nums = [1]

Output: 0

Explanation: With only one entry, the maximum and minimum are already equal.

Constraints:

  • 1 <= nums.length <= 100000
  • -1000000000 <= nums[i] <= 1000000000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.