LeetCode-in-Java

3736. Minimum Moves to Equal Array Elements III

Easy

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:

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:

Now, all elements of nums are equal to 5. The minimum total moves is 2.

Constraints:

Solution

public class Solution {
    public int minMoves(int[] nums) {
        int max = Integer.MIN_VALUE;
        int sum = 0;
        for (int num : nums) {
            sum += num;
            if (num > max) {
                max = num;
            }
        }
        return max * nums.length - sum;
    }
}