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.
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.
Example 1:
Input: nums = [5,3,2,4] Output: 0 Explanation: We can make at most 3 moves. In the first move, change 2 to 3. nums becomes [5,3,3,4]. In the second move, change 4 to 3. nums becomes [5,3,3,3]. In the third move, change 5 to 3. nums becomes [3,3,3,3]. After performing 3 moves, the difference between the minimum and maximum is 3 - 3 = 0.
Example 2:
Input: nums = [1,5,0,10,14] Output: 1 Explanation: We can make at most 3 moves. In the first move, change 5 to 0. nums becomes [1,0,0,10,14]. In the second move, change 10 to 0. nums becomes [1,0,0,0,14]. In the third move, change 14 to 1. nums becomes [1,0,0,0,1]. After performing 3 moves, the difference between the minimum and maximum is 1 - 0 = 1. It can be shown that there is no way to make the difference 0 in 3 moves.
Example 3:
Input: nums = [3,100,20] Output: 0 Explanation: We can make at most 3 moves. In the first move, change 100 to 7. nums becomes [3,7,20]. In the second move, change 20 to 7. nums becomes [3,7,7]. In the third move, change 3 to 7. nums becomes [7,7,7]. After performing 3 moves, the difference between the minimum and maximum is 7 - 7 = 0.
Constraints:
1 <= nums.length <= 105-109 <= nums[i] <= 109Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[5,3,2,4]
[1,5,0,10,14]
[3,100,20]
minimize-the-maximum-difference-of-pairs)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1509: Minimum Difference Between Largest and Smallest Value in Three Moves
class Solution {
public int minDifference(int[] nums) {
int n = nums.length;
if (n < 5) {
return 0;
}
Arrays.sort(nums);
long ans = 1L << 60;
for (int l = 0; l <= 3; ++l) {
int r = 3 - l;
ans = Math.min(ans, (long) nums[n - 1 - r] - nums[l]);
}
return (int) ans;
}
}
// Accepted solution for LeetCode #1509: Minimum Difference Between Largest and Smallest Value in Three Moves
func minDifference(nums []int) int {
n := len(nums)
if n < 5 {
return 0
}
sort.Ints(nums)
ans := 1 << 60
for l := 0; l <= 3; l++ {
r := 3 - l
ans = min(ans, nums[n-1-r]-nums[l])
}
return ans
}
# Accepted solution for LeetCode #1509: Minimum Difference Between Largest and Smallest Value in Three Moves
class Solution:
def minDifference(self, nums: List[int]) -> int:
n = len(nums)
if n < 5:
return 0
nums.sort()
ans = inf
for l in range(4):
r = 3 - l
ans = min(ans, nums[n - 1 - r] - nums[l])
return ans
// Accepted solution for LeetCode #1509: Minimum Difference Between Largest and Smallest Value in Three Moves
struct Solution;
impl Solution {
fn min_difference(mut nums: Vec<i32>) -> i32 {
let n = nums.len();
if n <= 3 {
return 0;
}
nums.sort_unstable();
let mut res = std::i32::MAX;
for i in 0..=3 {
let min = nums[i..n - (3 - i)].iter().min().unwrap();
let max = nums[i..n - (3 - i)].iter().max().unwrap();
res = res.min(max - min);
}
res
}
}
#[test]
fn test() {
let nums = vec![5, 3, 2, 4];
let res = 0;
assert_eq!(Solution::min_difference(nums), res);
let nums = vec![1, 5, 0, 10, 14];
let res = 1;
assert_eq!(Solution::min_difference(nums), res);
let nums = vec![6, 6, 0, 1, 1, 4, 6];
let res = 2;
assert_eq!(Solution::min_difference(nums), res);
let nums = vec![1, 5, 6, 14, 15];
let res = 1;
assert_eq!(Solution::min_difference(nums), res);
}
// Accepted solution for LeetCode #1509: Minimum Difference Between Largest and Smallest Value in Three Moves
function minDifference(nums: number[]): number {
if (nums.length < 5) {
return 0;
}
nums.sort((a, b) => a - b);
let ans = Number.POSITIVE_INFINITY;
for (let i = 0; i < 4; i++) {
ans = Math.min(ans, nums.at(i - 4)! - nums[i]);
}
return ans;
}
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.