Minimum Difference Between Largest And Smallest in Three Moves
Problem Statementβ
You are given an integer array nums
.
In one move, you can choose one element of nums
and change it to any value.
Return the minimum difference between the largest and smallest value of nums
after performing at most three moves.
Examplesβ
Example 1β
Input: nums = [5,3,2,4]
Output: 0
Explanation:
- Change 2 to 3:
nums
becomes[5,3,3,4]
. - Change 4 to 3:
nums
becomes[5,3,3,3]
. - Change 5 to 3:
nums
becomes[3,3,3,3]
.
After 3 moves, the difference between the minimum and maximum is3 - 3 = 0
.
Example 2β
Input: nums = [1,5,0,10,14]
Output: 1
Explanation:
- Change 5 to 0:
nums
becomes[1,0,0,10,14]
. - Change 10 to 0:
nums
becomes[1,0,0,0,14]
. - Change 14 to 1:
nums
becomes[1,0,0,0,1]
.
After 3 moves, the difference between the minimum and maximum is1 - 0 = 1
.
Example 3β
Input: nums = [3,100,20]
Output: 0
Explanation:
- Change 100 to 7:
nums
becomes[3,7,20]
. - Change 20 to 7:
nums
becomes[3,7,7]
. - Change 3 to 7:
nums
becomes[7,7,7]
.
After 3 moves, the difference between the minimum and maximum is7 - 7 = 0
.
Constraintsβ
Algorithmβ
- If the length of
nums
is less than or equal to 4, return 0 because we can change all elements to be the same. - Sort the array
nums
. - Consider the 4 smallest and 4 largest elements from the sorted array:
- The potential moves would be:
- Remove the 3 largest elements and keep the smallest 1.
- Remove the 2 largest elements and the smallest 1, keeping the next smallest 2.
- Remove the 1 largest element and the smallest 2, keeping the next smallest 3.
- Remove the 3 smallest elements and keep the largest 1.
- The potential moves would be:
- Return the minimum difference among these potential moves.
C++ Codeβ
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int minDifference(vector<int>& nums) {
int n = nums.size();
if (n <= 4) return 0;
sort(nums.begin(), nums.end());
int result = INT_MAX;
for (int i = 0; i <= 3; ++i) {
result = min(result, nums[n - 4 + i] - nums[i]);
}
return result;
}
};
Python Codeβ
class Solution:
def minDifference(self, nums: List[int]) -> int:
n = len(nums)
if n <= 4:
return 0
nums.sort()
return min(nums[-4 + i] - nums[i] for i in range(4))
Java Codeβ
import java.util.Arrays;
class Solution {
public int minDifference(int[] nums) {
int n = nums.length;
if (n <= 4) return 0;
Arrays.sort(nums);
int result = Integer.MAX_VALUE;
for (int i = 0; i <= 3; ++i) {
result = Math.min(result, nums[n - 4 + i] - nums[i]);
}
return result;
}
}
JavaScript Codeβ
/**
* @param {number[]} nums
* @return {number}
*/
var minDifference = function (nums) {
const n = nums.length;
if (n <= 4) return 0;
nums.sort((a, b) => a - b);
let result = Infinity;
for (let i = 0; i <= 3; ++i) {
result = Math.min(result, nums[n - 4 + i] - nums[i]);
}
return result;
};