Minimum Moves to Equal Array Elements

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.

30:00

Minimum Moves to Equal Array Elements
medium
Free written solution
Topics
Companies

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
Constraints:
  • n=nums.lengthn = \text{nums.length}

  • 1≤n≤1051 \leq n \leq 10^5

  • −109≤nums[i]≤109-10^9 \leq \text{nums[i]} \leq 10^9

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

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,2,3]}

One move adds 1 to 2 of the 3 elements. What is the fewest moves that makes all 3 equal?

- - minimum■ height above the minimum = moves needed
123

moves so far

0

Variables
VariableValue
n3
low-
x-
moves0
DepthFunction Call
Stack empty
0/7