greedy October 18, 2022

Reduction operations to make the array elements equal

Time O(nlog(n)) Space O(1) Open original problem

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)