Talentd
FleetCode

3736. Minimum Moves to Equal Array Elements III

Easy81.0% acceptance1 company

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

Asked at 1 company

Topics

Hints open one at a time

Hint 1
To make all numbers equal, they must all reach at least the maximum value in the array.
1:58
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def minMoves(self, nums: List[int]) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[2,1,3]
Input
[4,4,5]
Your code is saved on this device.Solution