Reduction Operations to Make the Array Elements Equal
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
numsstrictly 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
3It takes 3 operations to reduce 5 to 3, then both 3s to 1, making all elements equal.
Example 2
Input
nums = [1,1,1]Output
0All elements in nums are already equal.
Constraints
- 1 <= nums.length <= 5 * 10^4
- 1 <= nums[i] <= 5 * 10^4