
Minimum Difference Between Largest and Smallest Value in Three Moves (medium)
Problem Statement
You are given an integer array nums. You're allowed to change any of the numbers up to three times, turning them into any value you wish.
Return the minimum difference between the largest and smallest value of nums after updating the nums array.
Examples
-
Example 1:
- Input: [1,5,6,14,15]
- Expected Output: 1
- Justification: Change the numbers 14 and 15 to 2 and 3, respectively (or similarly small numbers). Now, the array is [1,2,3,5,6] with a minimum difference of 5 - 1 = 4.
-
Example 2:
- Input: [10,10,10,10,10]
- Expected Output: 0
- Justification: All numbers are the same, so even without any moves, the difference is already 0.
-
Example 3:
- Input: [1,100,200,300,400]
- Expected Output: 99
- Justification: Change the three highest values to any number between 1 and 100 (inclusive). For simplicity, changing 200, 300, and 400 to 2, 3, and 4 respectively will make the array [1,2,3,4,100], with a minimum difference of 100 - 1 = 99.
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory