> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/453-minimum-moves-to-equal-array-elements.md).

# 453 Minimum Moves to Equal Array Elements

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.

Example:

```
Input:
[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]
```

**The Idea:** To simplify the problem a little bit, think of the equivalent complement. If our goal is to increment all but one until all elements are equal, that is the same as decrementing 1 number until all numbers are the same. The brute force method by the way, is counting the number times you can increment the two smallest numbers until all the elements are the same. In the same way, how many times can we decrement until we reach the minimum? Well this would be all the elements summed up, but the minimum element, and then we would subtract this by the length of the array \* the minimum element. The first part counts the number of decrements to zero. The second bit accounts for the fact that we only want to go to the minimum.

**Complexity:** O(n) time, constant space

```
def minMoves(self, nums):
    """
    :type nums: List[int]
    :rtype: int
    """

    m = min(nums)
    dec_2_zero = sum(nums) - m
    over_shoot = (len(nums)-1) * m
    return dec_2_zero - over_shoot
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/453-minimum-moves-to-equal-array-elements.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
