Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an integer array nums.
nums is the minimum absolute difference between any two integers.nums is the maximum absolute difference between any two integers.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:
nums[0] and nums[1] to be 6 so that nums becomes [6,6,7,8,5].Example 2:
Input: nums = [1,4,3]
Output: 0
Explanation:
nums[1] and nums[2] to 1 so that nums becomes [1,1,1].Constraints:
3 <= nums.length <= 1051 <= nums[i] <= 109Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[1,4,7,8,5]
[1,4,3]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2567: Minimum Score by Changing Two Elements
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));
}
}
// Accepted solution for LeetCode #2567: Minimum Score by Changing Two Elements
func minimizeSum(nums []int) int {
sort.Ints(nums)
n := len(nums)
return min(nums[n-1]-nums[2], min(nums[n-2]-nums[1], nums[n-3]-nums[0]))
}
# Accepted solution for LeetCode #2567: Minimum Score by Changing Two Elements
class Solution:
def minimizeSum(self, nums: List[int]) -> int:
nums.sort()
return min(nums[-1] - nums[2], nums[-2] - nums[1], nums[-3] - nums[0])
// Accepted solution for LeetCode #2567: Minimum Score by Changing Two Elements
impl Solution {
pub fn minimize_sum(mut nums: Vec<i32>) -> i32 {
nums.sort();
let n = nums.len();
(nums[n - 1] - nums[2])
.min(nums[n - 2] - nums[1])
.min(nums[n - 3] - nums[0])
}
}
// Accepted solution for LeetCode #2567: Minimum Score by Changing Two Elements
function minimizeSum(nums: number[]): number {
nums.sort((a, b) => a - b);
const n = nums.length;
return Math.min(nums[n - 3] - nums[0], nums[n - 2] - nums[1], nums[n - 1] - nums[2]);
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.