Talentd
FleetCode
Make and follow sheets.Sign in

Minimum Moves to Equal Array Elements solution and explanation

MediumArrayMath5 min read59.2% acceptanceUpdated 9 Apr 2026
453. Minimum Moves to Equal Array Elements LeetCodehakunamatasq · 3:02 · 7K viewsWatch on YouTube
9 more videos on this problem
  1. 453. Minimum Moves to Equal Array Elements LeetCodehakunamatasq · 3:02 · 7K views
  2. Leetcode 453 Minimum Moves to Equal Array Elements - Coursera Interview Problem Solved In O(N) TimeAlgorithmicIQ · 8:24 · 6K views
  3. Minimum Moves to Equal Array Elements | LintCode 1231|Leetcode 453LIT2Me · 16:40 · 1K views
  4. leetCode 453 - Minimum Moves to Equal Array Elements | JSer - algorithm & JavaScriptJSer · 45:27 · 1K views
  5. 453. Minimum Moves to Equal Array Elements (Leetcode Medium)Programming Live with Larry · 5:46 · 1K views
  6. (C#)LeetCode 453 Minimum Moves to Equal Array Elementszongwu yang · 2:13 · 869 views
  7. LeetCode 453 | Minimum Moves to Equal Array Elements | Math JavaSleepy Cracker · 3:14 · 729 views
  8. Minimum Moves to Equal Array Elements #leetcode #453 #solution easy solution with explanationCoding Champs · 11:16 · 322 views
  9. Leetcode 453.Minimum Move to Equal Array Element|Dry Run| #solution Explained|#interview |#productsYouth Family-Eduketa · 6:10 · 68 views
  10. Leetcode program | Minimum move to equal array elements | Java Leetcode programming exerciseSoftWiz Circle · 3:41 · 116 views

The problem

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.length
  • 1 <= nums.length <= 105
  • -109 <= nums[i] <= 109
  • The answer is guaranteed to fit in a 32-bit integer.

Approach overview

Problem Overview: You’re given an integer array nums. One move lets you increment n − 1 elements by 1. The task is to compute the minimum number of moves required so that all elements become equal.

Approach 1: Brute Force Simulation (O(n * moves) time, O(1) space)

A direct interpretation is to repeatedly increment all elements except the current maximum until every value becomes equal. Each iteration scans the array, finds the maximum element, and increments the remaining n-1 elements. This continues until the array converges to a single value. While this matches the problem statement exactly, the number of operations can grow very large because the gap between elements shrinks slowly. Every move requires a full iteration through the array, making this approach impractical for large inputs.

Approach 2: Mathematical Observation (O(n) time, O(1) space)

The key insight flips the perspective. Incrementing n-1 elements by 1 is equivalent to decrementing a single element by 1. Instead of thinking about raising most numbers, imagine reducing the largest values until all elements match the smallest element in the array. This observation turns the process into a simple counting problem.

If the minimum element in the array is minVal, every other element must be reduced until it equals minVal. The number of required operations for each element is simply nums[i] - minVal. Summing these differences across the entire array gives the total number of moves:

moves = sum(nums) - n * min(nums)

This works because each simulated "decrement" corresponds exactly to one valid operation in the original problem. The algorithm only needs a single pass to compute the array sum and minimum value, making it optimal for large inputs.

This approach relies purely on arithmetic reasoning rather than explicit simulation. You iterate through the array once, track the minimum value, compute the total sum, and apply the formula. No extra data structures are required.

Conceptually, this problem blends math reasoning with simple iteration over an array. Many interview problems follow the same pattern: a naive simulation exists, but a mathematical invariant leads to a much faster solution.

Recommended for interviews: The mathematical observation approach is what interviewers expect. Explaining the equivalence between "increment n−1 elements" and "decrement one element" demonstrates problem‑solving insight. Mentioning the brute force idea first shows you understand the problem mechanics, then deriving the formula shows strong algorithmic reasoning. The optimal solution runs in O(n) time with O(1) extra space.

Approach 1: Approach 1: Mathematical Observation

The problem can be simplified by observing that incrementing n-1 elements is equivalent to decrementing 1 element. Thus, the number of moves required to make all elements in the array equal is equal to the total number of moves required to make all elements equal to the minimum element present in the array.

To achieve this, calculate the difference between each element and the minimum element, summing these differences yields the required number of moves.

def min_moves(nums):
    min_element = min(nums)
    return sum(num - min_element for num in nums)

nums = [1, 2, 3]
print(min_moves(nums))
import java.util.*;

public class Main {
    public static int minMoves(int[] nums) {
        int min = Arrays.stream(nums).min().getAsInt();
        int moves = 0;
        for (int num : nums) {
            moves += num - min;
        }
        return moves;
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        System.out.println(minMoves(nums));
    }
}
#include <iostream>
#include <vector>
#include <algorithm>

int minMoves(std::vector<int>& nums) {
    int minVal = *std::min_element(nums.begin(), nums.end());
    int moves = 0;
    for (int num : nums) {
        moves += num - minVal;
    }
    return moves;
}

int main() {
    std::vector<int> nums = {1, 2, 3};
    std::cout << minMoves(nums) << std::endl;
    return 0;
}
function minMoves(nums) {
    let min = Math.min(...nums);
    return nums.reduce((acc, num) => acc + num - min, 0);
}

console.log(minMoves([1, 2, 3]));
#include <stdio.h>
#include <limits.h>

int minMoves(int* nums, int numsSize) {
    int min = INT_MAX;
    int moves = 0;
    for (int i = 0; i < numsSize; i++) {
        if (nums[i] < min) {
            min = nums[i];
        }
    }
    for (int i = 0; i < numsSize; i++) {
        moves += nums[i] - min;
    }
    return moves;
}

int main() {
    int nums[] = {1, 2, 3};
    printf("%d\n", minMoves(nums, 3));
    return 0;
}
using System;
using System.Linq;

public class Program {
    public static int MinMoves(int[] nums) {
        int min = nums.Min();
        return nums.Sum(num => num - min);
    }

    public static void Main() {
        int[] nums = {1, 2, 3};
        Console.WriteLine(MinMoves(nums));
    }
}

The Python solution leverages Python's built-in functions to determine the minimum and accumulate the differences succinctly.

Complexity

Time Complexity: O(n), Space Complexity: O(1)

Try this approach in the editor

Approach 2: Mathematics

Let the minimum value of the array nums be mi, the sum of the array be s, and the length of the array be n.

Assume the minimum number of operations is k, and the final value of all elements in the array is x. Then we have:

$ \begin{aligned} s + (n - 1) times k &= n times x \ x &= mi + k \ \end{aligned}

Substituting the second equation into the first equation, we get:

\begin{aligned} s + (n - 1) times k &= n times (mi + k) \ s + (n - 1) times k &= n times mi + n times k \ k &= s - n times mi \ \end{aligned}

Therefore, the minimum number of operations is s - n times mi.

The time complexity is O(n), and the space complexity is O(1). Here, n$ is the length of the array.

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;
}
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)
}
Try this approach in the editor

Complexity comparison

ApproachComplexity
Approach 1: Mathematical Observation

Time Complexity: O(n), Space Complexity: O(1)

Mathematics–

Complexity analysis

ApproachTimeSpaceWhen to Use
Brute Force SimulationO(n * moves)O(1)Useful for understanding the mechanics of the operation but impractical for large arrays.
Mathematical ObservationO(n)O(1)Optimal solution for all cases. Uses array sum and minimum value to compute moves directly.

Frequently asked questions

Is Minimum Moves to Equal Array Elements easy or hard?

The problem is rated Medium because the naive interpretation leads to inefficient simulation. The challenge is recognizing the mathematical transformation that converts the operation into reducing elements toward the minimum value.

Minimum Moves to Equal Array Elements Python/Java solution

Most implementations compute the array sum and minimum value in one pass. The result is returned using the formula sum(nums) − n × min(nums). The logic is identical across Python, Java, C++, C#, and JavaScript because it relies only on basic arithmetic.

How to solve Minimum Moves to Equal Array Elements in O(n)?

Scan the array once to compute the minimum element and the total sum. Each move effectively reduces one element toward the minimum. The total moves required equals sum(nums) − n × min(nums). This formula directly calculates the result without simulation, giving O(n) time and constant space.

What is the best approach for Minimum Moves to Equal Array Elements?

The best approach is a mathematical observation that converts the operation. Incrementing n−1 elements is equivalent to decrementing one element. This means all numbers effectively move toward the minimum element. The total moves equal sum(nums) − n × min(nums), which runs in O(n) time and O(1) space.

Is Minimum Moves to Equal Array Elements asked at Google/Amazon/Meta?

Array and mathematical reasoning problems like this frequently appear in interviews at companies such as Amazon, Google, and Meta. The question tests whether you recognize the invariant that converts repeated increments into a simpler arithmetic formula.

What data structure is used in Minimum Moves to Equal Array Elements?

The problem primarily uses a basic array. No advanced data structures are required. The optimal solution only iterates through the array while tracking the minimum value and total sum.

What is the time complexity of Minimum Moves to Equal Array Elements?

The optimal solution runs in O(n) time because it requires a single pass through the array to compute the total sum and the minimum value. Space complexity is O(1) since only a few variables are used regardless of input size.

Comments

Now write it yourselfClose this page and solve Minimum Moves to Equal Array Elements in the editor, without looking.
Practise Minimum Moves to Equal Array Elements