Menu

453. Minimum Moves to Equal Array Elements
📰

453. Minimum Moves to Equal Array Elements

DEV Community: php·MD ARIFUL HAQUE·17 days ago
#fMUJMlPi
#devto#dev#nums#example#input
Reading 0:00
15s threshold

453. Minimum Moves to Equal Array Elements

Difficulty: Medium

Topics: Junior , Array , Math

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

Example 3:

Input: nums = [5]

Output: 0

Example 4:

Input: nums = [-2,-1,0]

Output: 3

Example 5:

Input: nums = [0,10]

Output: 10

Example 6:

Input: nums = [5,5,10]

Output: 5

Example 7:

Input: nums = [1,2,100]

Output: 100

Example 8:

Input: nums = [-1000000000, 1000000000]

Output: 2000000000

Constraints:

n == nums.length

1 <= nums.length <= 10⁵

-10⁹ <= nums[i] <= 10⁹

The answer is guaranteed to fit in a 32-bit integer.

Similar Questions:

462. Minimum Moves to Equal Array Elements II

2141.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.