Make all array elements equal using the minimum number of reduction operations.
You are given an integer array nums. In one reduction operation, you may choose any element and decrease it to match the value of the next smaller distinct element present in the array.
Your task is to find the minimum number of reduction operations needed until all elements in the array become equal.
The key constraint is that you can only reduce a value to another value that already exists in the array and is strictly smaller than it.
Input Format
- An integer array
nums. - Each operation selects one element and lowers it to the next smaller distinct value currently present in the array.
Output Format
- Return the minimum number of reduction operations required to make all elements equal.
Constraints
1 <= nums.length- Elements are integers.
- Each operation must reduce an element to the next smaller distinct value among the array values.
- The final array must contain one common value in every position.
Example 1
Input
nums = [5,1,3]
Output
3
Explanation
One optimal sequence is: reduce 5 to 3, then reduce 3 to 1, and reduce the remaining 3 to 1. Total operations = 3.
Example 2
Input
nums = [1,1,1]
Output
0
Explanation
All elements are already equal, so no operations are needed.
Show 1 more example
Example 3
Input
nums = [1,1,2,2,3]
Output
4
Explanation
Reduce 3 to 2 once, then reduce both 2s to 1 in two operations, and reduce the remaining 2 to 1 in one more operation. Total operations = 4.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.