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.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given an integer array nums.
In one move, you may increase the value of any single element nums[i] by 1.
Return the minimum total number of moves required so that all elements in nums become equal.
Example 1:
Input: nums = [2,1,3]
Output: 3
Explanation:
To make all elements equal:
nums[0] = 2 by 1 to make it 3.nums[1] = 1 by 1 to make it 2.nums[1] = 2 by 1 to make it 3.Now, all elements of nums are equal to 3. The minimum total moves is 3.
Example 2:
Input: nums = [4,4,5]
Output: 2
Explanation:
To make all elements equal:
nums[0] = 4 by 1 to make it 5.nums[1] = 4 by 1 to make it 5.Now, all elements of nums are equal to 5. The minimum total moves is 2.
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 100Problem summary: You are given an integer array nums. In one move, you may increase the value of any single element nums[i] by 1. Return the minimum total number of moves required so that all elements in nums become equal.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[2,1,3]
[4,4,5]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3736: Minimum Moves to Equal Array Elements III
class Solution {
public int minMoves(int[] nums) {
int n = nums.length;
int mx = 0;
int s = 0;
for (int x : nums) {
mx = Math.max(mx, x);
s += x;
}
return mx * n - s;
}
}
// Accepted solution for LeetCode #3736: Minimum Moves to Equal Array Elements III
func minMoves(nums []int) int {
mx, s := 0, 0
for _, x := range nums {
mx = max(mx, x)
s += x
}
return mx*len(nums) - s
}
# Accepted solution for LeetCode #3736: Minimum Moves to Equal Array Elements III
class Solution:
def minMoves(self, nums: List[int]) -> int:
n = len(nums)
mx = max(nums)
s = sum(nums)
return mx * n - s
// Accepted solution for LeetCode #3736: Minimum Moves to Equal Array Elements III
fn min_moves(nums: Vec<i32>) -> i32 {
let max = *nums.iter().max().unwrap();
nums.into_iter().fold(0, |acc, n| acc + max - n)
}
fn main() {
let ret = min_moves(vec![2, 1, 3]);
println!("ret={ret}");
}
#[test]
fn test() {
assert_eq!(min_moves(vec![2, 1, 3]), 3);
assert_eq!(min_moves(vec![4, 4, 5]), 2);
}
// Accepted solution for LeetCode #3736: Minimum Moves to Equal Array Elements III
function minMoves(nums: number[]): number {
const n = nums.length;
const mx = Math.max(...nums);
const s = nums.reduce((a, b) => a + b, 0);
return mx * n - s;
}
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.