Given an integer array nums
, your goal is to make all elements in nums
equal. To complete one operation, follow these steps:
- Find the largest value in
nums
. Let its index bei
(0-indexed) and its value belargest
. If there are multiple elements with the largest value, pick the smallesti
. - Find the next largest value in
nums
strictly smaller thanlargest
. Let its value benextLargest
. - Reduce
nums[i]
tonextLargest
.
Return the number of operations to make all elements in nums
equal.
Example 1:
- Input:
nums = [5,1,3]
- Output: 3
- Explanation: It takes 3 operations to make all elements in nums equal:
largest = 5
at index 0.nextLargest = 3
. Reducenums[0]
to 3.nums = [3,1,3]
.largest = 3
at index 0.nextLargest = 1
. Reducenums[0]
to 1.nums = [1,1,3]
.largest = 3
at index 2.nextLargest = 1
. Reducenums[2]
to 1.nums = [1,1,1]
.