Skip to main content

Minimum Moves to Equal Array Elements III - Solution & Explanation

EasyArrayMath6 min readAsked at: Adobe
Practice this problem

Problem Statement

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:

  • Increase nums[0] = 2 by 1 to make it 3.
  • Increase nums[1] = 1 by 1 to make it 2.
  • Increase 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:

  • Increase nums[0] = 4 by 1 to make it 5.
  • Increase 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 <= 100
  • 1 <= nums[i] <= 100

Approach Overview

Problem Overview: You are given an integer array and can increment any element by 1 in a single move. The goal is to compute the minimum number of moves required so every value in the array becomes equal.

Approach 1: Try Every Possible Target Value (Brute Force) (Time: O(n * R), Space: O(1))

A straightforward idea is to try making every element equal to a candidate target value and count the moves needed. For each candidate target t, iterate through the array and sum t - nums[i] for elements smaller than t. The smallest total across all candidates is the answer. This works because each move increases an element by exactly one. The downside is efficiency. If the candidate range R is large (for example from the minimum to maximum value), the algorithm repeatedly scans the array and becomes unnecessarily slow.

Approach 2: Calculate Sum and Maximum Value (Optimal) (Time: O(n), Space: O(1))

The key observation is that when you can only increment elements, the final equal value must be at least the current maximum element. Choosing any value larger than the maximum would only add extra moves without benefit. Therefore, the optimal target is exactly the maximum value in the array.

Once you know the target, the number of moves required for each element is simply the difference between the maximum and that element. Summing these differences across the array gives the total moves. Instead of computing each difference separately, use a compact formula: moves = maxValue * n - sum(nums). One pass through the array gives both the total sum and the maximum value.

This approach relies on simple arithmetic and a single iteration over the array. No additional data structures are required. Problems like this commonly appear in interview questions involving array traversal and basic math reasoning.

Recommended for interviews: Interviewers expect the mathematical insight that the target value must be the maximum element. A brute force attempt shows understanding of the problem mechanics, but recognizing the max * n - sum relationship demonstrates strong problem‑solving and optimization skills.

Solution

This problem requires making all elements in the array equal, with each operation only able to increase a single element by 1. To minimize the number of operations, we should make all elements equal to the maximum value in the array.

Therefore, we can first calculate the maximum value mx and the sum of array elements s. The number of operations required to make all elements equal to mx is mx times n - s, where n is the length of the array.

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

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Try Every Target Value (Brute Force)O(n * R)O(1)Useful for understanding the mechanics of increment operations and verifying correctness on small ranges
Calculate Sum and Maximum ValueO(n)O(1)Optimal approach for large arrays; single pass using simple arithmetic

Video Solution

3736. Minimum Moves to Equal Array Elements III (Leetcode Easy) • Programming Live with Larry • 979 views views

Watch 3 more video solutions →

Frequently Asked Questions

Is Minimum Moves to Equal Array Elements III easy or hard?
Minimum Moves to Equal Array Elements III is considered an Easy problem. The challenge is recognizing that the optimal target value must be the maximum element, which simplifies the solution to a simple arithmetic formula.
Minimum Moves to Equal Array Elements III Python/Java solution
In Python, Java, C++, or Go, the implementation is identical in logic: iterate once to compute the sum and maximum value, then return maxValue * n - sum. The algorithm uses constant extra space and works efficiently for large arrays.
How to solve Minimum Moves to Equal Array Elements III in O(n)?
Iterate through the array once to compute the maximum element and the sum of all elements. The target value is the maximum because increments cannot reduce larger numbers. The minimum moves are calculated using the formula maxValue * n - totalSum.
What is the best approach for Minimum Moves to Equal Array Elements III?
The optimal solution computes the maximum element and the total sum of the array in one pass. Since elements can only be incremented, the final equal value must be the maximum value. The total moves are calculated using maxValue * n - sum(nums). This runs in O(n) time and O(1) space.
Is Minimum Moves to Equal Array Elements III asked at Google/Amazon/Meta?
Array and math optimization problems like this frequently appear in coding interviews at companies such as Amazon, Google, and Meta. The question tests whether you can derive a mathematical shortcut instead of simulating operations.
What data structure is used in Minimum Moves to Equal Array Elements III?
The problem only requires basic array traversal. No advanced data structures are needed. The solution tracks two values while iterating: the running sum and the current maximum.
What is the time complexity of Minimum Moves to Equal Array Elements III?
The optimal algorithm runs in O(n) time because it scans the array once to compute the sum and maximum value. Space complexity is O(1) since only a few variables are used regardless of input size.

Ready to solve this problem?

Practice Minimum Moves to Equal Array Elements III with our built-in code editor and test cases.

Practice on FleetCode