class Solution:
    def reductionOperations(self, nums: List[int]) -> int:
        return sum(accumulate(c for _,c in sorted(Counter(nums).items(), reverse=True)[:-1]))
class Solution {
    public int reductionOperations(int[] nums) {
        Map<Integer, Integer> valMap = new TreeMap<>(Collections.reverseOrder());

        for (int i=0; i<nums.length; i++)
            valMap.put(nums[i], valMap.getOrDefault(nums[i], 0) + 1);

        int mapSize = valMap.size();
        int opsCount = 0;
        for (Map.Entry<Integer, Integer> entry : valMap.entrySet()) {
            opsCount += entry.getValue() * (--mapSize);
        }
        return opsCount;
    }
}
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 be i (0-indexed) and its value be largest. If there are multiple elements with the largest value, pick the smallest i.
	Find the next largest value in nums strictly smaller than largest. Let its value be nextLargest.
	Reduce nums[i] to nextLargest.


Return the number of operations to make all elements in nums equal.

&nbsp;
Example 1:

Input: nums = [5,1,3]
Output: 3
Explanation:&nbsp;It takes 3 operations to make all elements in nums equal:
1. largest = 5 at index 0. nextLargest = 3. Reduce nums[0] to 3. nums = [3,1,3].
2. largest = 3 at index 0. nextLargest = 1. Reduce nums[0] to 1. nums = [1,1,3].
3. largest = 3 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1].


Example 2:

Input: nums = [1,1,1]
Output: 0
Explanation:&nbsp;All elements in nums are already equal.


Example 3:

Input: nums = [1,1,2,2,3]
Output: 4
Explanation:&nbsp;It takes 4 operations to make all elements in nums equal:
1. largest = 3 at index 4. nextLargest = 2. Reduce nums[4] to 2. nums = [1,1,2,2,2].
2. largest = 2 at index 2. nextLargest = 1. Reduce nums[2] to 1. nums = [1,1,1,2,2].
3. largest = 2 at index 3. nextLargest = 1. Reduce nums[3] to 1. nums = [1,1,1,1,2].
4. largest = 2 at index 4. nextLargest = 1. Reduce nums[4] to 1. nums = [1,1,1,1,1].


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 5 * 104
	1 &lt;= nums[i] &lt;= 5 * 104


/**
 * @param {number[]} nums
 * @return {number}
 */
var reductionOperations = function(nums) {
   nums.sort((a,b)=>a-b);
    let count = 0;
    for(let i = nums.length - 1;i>0;i--)
        if(nums[i] !== nums[i-1])
            count += nums.length - i
    return count
};
class Solution {
public:
    int reductionOperations(vector<int>& nums) {
        int n = nums.size();
        
        map<int, int> mp;
        for(int i = 0; i < n; i ++) {
            mp[nums[i]] ++;             // storing the frequency
        }
        
        int ans = 0;
        int pre = 0;
        for (auto i = mp.end(); i != mp.begin(); i--) {
            ans += i -> second + pre;   // total operations
            pre += i -> second;         // maintaing the previous frequency count
        }
        return ans;
    }
};