Menu

462. Minimum Moves to Equal Array Elements II
📰

462. Minimum Moves to Equal Array Elements II

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

462. Minimum Moves to Equal Array Elements II

Difficulty: Medium

Topics: Principal , Array , Math , Sorting

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 or decrement an element of the array by 1 .

Test cases are designed so that the answer will fit in a 32-bit integer.

Example 1:

Input: nums = [1,2,3]

Output: 2

Explanation: Only two moves are needed (remember each move increments or decrements one element): [1,2,3] => [2,2,3] => [2,2,2]

Example 2:

Input: nums = [1,10,2,9]

Output: 16

Example 3:

Input: nums = [1]

Output: 0

Example 4:

Input: nums = [1,1,1]

Output: 0

Example 5:

Input: nums = [0,0,10,10]

Output: 20

Example 6:

Input: nums = [-5,-1,3]

Output: 8

Example 7:

Input: nums = [1,2,10]

Output: 9

Example 8:

Input: nums = [1,2,3,4,5]

Output: 6

Example 9:

Input: nums = [-1000000000, 1000000000]

Output: 2000000000

Example 10:

Input: nums = [5,5,5,5,5]

Output:…

Continue reading — create a free account

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