2567. Minimum Score by Changing Two Elements LeetCode Solution
In this guide, you will get 2567. Minimum Score by Changing Two Elements LeetCode Solution with the best time and space complexity. The solution to Minimum Score by Changing Two Elements problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Score by Changing Two Elements solution in C++
- Minimum Score by Changing Two Elements solution in Java
- Minimum Score by Changing Two Elements solution in Python
- Additional Resources

Problem Statement of Minimum Score by Changing Two Elements
You are given an integer array nums.
The low score of nums is the minimum absolute difference between any two integers.
The high score of nums is the maximum absolute difference between any two integers.
The score of nums is 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] and nums[1] to be 6 so that nums becomes [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] and nums[2] to 1 so that nums becomes [1,1,1].
The sum of maximum absolute difference and minimum absolute difference is 0.
Constraints:
3 <= nums.length <= 105
1 <= nums[i] <= 109
Complexity Analysis
- Time Complexity: O(\texttt{sort})
- Space Complexity: O(\texttt{sort})
2567. Minimum Score by Changing Two Elements LeetCode Solution in C++
class Solution {
public:
int minimizeSum(vector<int>& nums) {
ranges::sort(nums);
// Can always change the number to any other number in `nums`, so `low`
// becomes 0. Thus, rephrase the problem as finding the minimum `high`.
const int n = nums.size();
const int highOfChangingTwoMins = nums.back() - nums[2];
const int highOfChangingTwoMaxs = nums[n - 3] - nums[0];
const int highOfChangingMinAndMax = nums[n - 2] - nums[1];
return min({highOfChangingTwoMins, highOfChangingTwoMaxs,
highOfChangingMinAndMax});
}
};
/* code provided by PROGIEZ */
2567. Minimum Score by Changing Two Elements LeetCode Solution in Java
class Solution {
public int minimizeSum(int[] nums) {
Arrays.sort(nums);
// Can always change the number to any other number in `nums`, so `low` becomes 0.
// Thus, rephrase the problem as finding the minimum `high`.
final int n = nums.length;
final int highOfChangingTwoMins = nums[n - 1] - nums[2];
final int highOfChangingTwoMaxs = nums[n - 3] - nums[0];
final int highOfChangingMinAndMax = nums[n - 2] - nums[1];
return Math.min(Math.min(highOfChangingTwoMins, highOfChangingTwoMaxs),
highOfChangingMinAndMax);
}
}
// code provided by PROGIEZ
2567. Minimum Score by Changing Two Elements LeetCode Solution in Python
class Solution:
def minimizeSum(self, nums: list[int]) -> int:
nums.sort()
# Can always change the number to any other number in `nums`, so `low` becomes 0.
# Thus, rephrase the problem as finding the minimum `high`.
highOfChangingTwoMins = nums[-1] - nums[2]
highOfChangingTwoMaxs = nums[-3] - nums[0]
highOfChangingMinAndMax = nums[-2] - nums[1]
return min(highOfChangingTwoMins, highOfChangingTwoMaxs,
highOfChangingMinAndMax)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.