#453

Minimum Moves to Equal Array Elements

specialist · 795 · lc medium +31 · verified · 58.6% accepted · 2,815 likes · top 55%

play →

Description

Given an integer array nums of size n, in a single operation you increment exactly n - 1 of the elements by 1. Determine the minimum number of operations needed to make every element in nums equal.

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

Code

1
2
3