Minimum Score by Changing Two Elements
MedPrompt
You are given an integer array nums.
- The low score of
numsis the minimum absolute difference between any two integers. - The high score of
numsis the maximum absolute difference between any two integers. - The score of
numsis the sum of the high and low scores.
Return the minimum score after changing two elements of nums.
Example 1:
Input: nums = [1,4,7,8,5]
Output: 3
Explanation:
- Change
nums[0]andnums[1]to be 6 so thatnumsbecomes [6,6,7,8,5]. - The low score is the minimum absolute difference: |6 - 6| = 0.
- The high score is the maximum absolute difference: |8 - 5| = 3.
- The sum of high and low score is 3.
Example 2:
Input: nums = [1,4,3]
Output: 0
Explanation:
- Change
nums[1]andnums[2]to 1 so thatnumsbecomes [1,1,1]. - The sum of maximum absolute difference and minimum absolute difference is 0.
Constraints:
3 <= nums.length <= 1051 <= nums[i] <= 109
Approaches
3 approaches with complexity analysis and trade-offs.
A significantly more efficient approach involves sorting the array first. To minimize the difference between the maximum and minimum elements, we must alter the current extremes. After sorting, the smallest and largest elements are easy to identify. We only need to consider three strategic choices for which two elements to change: the two smallest, the two largest, or the single smallest and single largest.
Algorithm
- Sort the input array
nums. - Let
nbe the length of the array. - If
n <= 3, return 0. - Calculate three potential scores:
score1 = nums[n-1] - nums[2](changing the two smallest).score2 = nums[n-3] - nums[0](changing the two largest).score3 = nums[n-2] - nums[1](changing one smallest, one largest).
- Return the minimum of
score1,score2, andscore3.
Walkthrough
The key insight is that the high score is determined by max(nums) - min(nums). To reduce this gap, we must change either the minimum element, the maximum element, or both. With two changes available, we have a few powerful options:
- Change the two smallest elements: By sorting the array, these are
nums[0]andnums[1]. We can change them to matchnums[2]. The new minimum becomesnums[2], and the maximum remainsnums[n-1]. The new high score isnums[n-1] - nums[2]. - Change the two largest elements: These are
nums[n-1]andnums[n-2]. We change them to matchnums[n-3]. The new maximum becomesnums[n-3], and the minimum remainsnums[0]. The new high score isnums[n-3] - nums[0]. - Change the smallest and the largest element: These are
nums[0]andnums[n-1]. We changenums[0]tonums[1]andnums[n-1]tonums[n-2]. The new range is fromnums[1]tonums[n-2]. The new high score isnums[n-2] - nums[1].
The minimum of these three resulting scores is our answer. As in the previous approach, the low score can be made 0 in each case.
import java.util.Arrays; class Solution { public int minimizeSum(int[] nums) { int n = nums.length; if (n <= 3) { return 0; } Arrays.sort(nums); // Case 1: Change the two smallest elements. // New range: nums[2] to nums[n-1] int score1 = nums[n - 1] - nums[2]; // Case 2: Change the two largest elements. // New range: nums[0] to nums[n-3] int score2 = nums[n - 3] - nums[0]; // Case 3: Change the smallest and the largest element. // New range: nums[1] to nums[n-2] int score3 = nums[n - 2] - nums[1]; return Math.min(score1, Math.min(score2, score3)); }}Complexity
Time
O(N log N), dominated by the sorting algorithm.
Space
O(log N) or O(N), depending on the space used by the sorting algorithm. In Java, `Arrays.sort` for primitives has an average space complexity of O(log N).
Trade-offs
Pros
Much more efficient than the brute-force approach.
The logic is clear and relatively easy to implement once the core idea is understood.
Cons
The
O(N log N)time complexity is not optimal, as sorting the entire array is more work than necessary.
Solutions
Solution
class Solution {public int minimizeSum(int[] nums) { Arrays.sort(nums); int n = nums.length; int a = nums[n - 1] - nums[2]; int b = nums[n - 2] - nums[1]; int c = nums[n - 3] - nums[0]; return Math.min(a, Math.min(b, c)); }}Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.