Reduction operations to make the array elements equal

October 18, 2022

greedy

Problem URL: Reduction operations to make the array elements equal

We will sort the array in descending order, then iterate over the array and count the number of elements that are less than the current element. Finally return the sum of the counts.

class Solution:
    def reductionOperations(self, nums: List[int]) -> int:
        nums.sort()
        n = len(nums)
        res = 0
        for i in range(n-1):
            if nums[i] != nums[i+1]:
                res += n-1-i
        return res

Time Complexity: O(nlog(n))
Space Complexity: O(1)