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.
Given an integer array nums of size n, return the minimum number of moves required to make all array elements equal.
In one move, you can increment n - 1 elements of the array by 1.
Example 1:
Input: nums = [1,2,3] Output: 3 Explanation: Only three moves are needed (remember each move increments two elements): [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
Example 2:
Input: nums = [1,1,1] Output: 0
Constraints:
n == nums.length1 <= nums.length <= 105-109 <= nums[i] <= 109Problem summary: Given an integer array nums of size n, return the minimum number of moves required to make all array elements equal. In one move, you can increment n - 1 elements of the array by 1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[1,2,3]
[1,1,1]
minimum-moves-to-equal-array-elements-ii)maximum-running-time-of-n-computers)pour-water-between-buckets-to-make-water-levels-equal)divide-players-into-teams-of-equal-skill)find-minimum-operations-to-make-all-elements-divisible-by-three)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #453: Minimum Moves to Equal Array Elements
class Solution {
public int minMoves(int[] nums) {
return Arrays.stream(nums).sum() - Arrays.stream(nums).min().getAsInt() * nums.length;
}
}
// Accepted solution for LeetCode #453: Minimum Moves to Equal Array Elements
func minMoves(nums []int) int {
mi := 1 << 30
s := 0
for _, x := range nums {
s += x
if x < mi {
mi = x
}
}
return s - mi*len(nums)
}
# Accepted solution for LeetCode #453: Minimum Moves to Equal Array Elements
class Solution:
def minMoves(self, nums: List[int]) -> int:
return sum(nums) - min(nums) * len(nums)
// Accepted solution for LeetCode #453: Minimum Moves to Equal Array Elements
struct Solution;
impl Solution {
fn min_moves(nums: Vec<i32>) -> i32 {
let mut min = nums[0];
let mut sum = 0;
for &x in &nums {
min = i32::min(x, min);
}
for &x in &nums {
sum += x - min;
}
sum
}
}
#[test]
fn test() {
let nums = vec![1, 2, 3];
assert_eq!(Solution::min_moves(nums), 3);
}
// Accepted solution for LeetCode #453: Minimum Moves to Equal Array Elements
function minMoves(nums: number[]): number {
let mi = 1 << 30;
let s = 0;
for (const x of nums) {
s += x;
mi = Math.min(mi, x);
}
return s - mi * nums.length;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
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: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.