Python3 Combine { sorting, binary search, prefix sums } O(nlgn) + O(qlgn) time O(N) Space Iterative
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3 []\n\'\'\'\nURL := https://leetcode.com/problems/minimum-operations-to-make-all-array-elements-equal/description/\n2602. Minimum Operations to Make All Array Elements Equal\n\nCategories : Sort, Greedy, Binary Search, Arithemtic, Counting\n\nComplexity\nN := len(nums) and Q := len(queries)\nT = O(NlgN) + O(QlgN)\nS = O(Q) ( Explicit ) O(1) ( Implicit  )\n\n20 minutes and passed :-)\ndo better with prefix sums?\nCorrect but TLE -> we\'re gonna need prefix summations now :-) \n\'\'\'\nclass Solution:\n    def minOperations(self, nums: List[int], queries: List[int]) -> List[int]:\n        nums.sort()\n        m = len(nums)\n        answer = [0 for idx in range(len(queries))]\n        prefixSums = [0 for idx in range(len(nums))]\n        prefixSumsRight = [0 for idx in range(len(nums))]\n        runSum = 0\n        for index, num in enumerate(nums):\n            runSum += num\n            prefixSums[index] = runSum\n\n        runSum = 0\n        for index, num in reversed(list(enumerate(nums))):\n            runSum += num\n            prefixSumsRight[index] = runSum\n\n        for index, query in enumerate(queries):\n            deltaLower = 0\n            deltaUpper = 0\n            lowerSum = 0\n            upperSum  = 0\n\n            lowerIdx = self.bSearchLower(nums, query)\n            if(lowerIdx != -1):\n                lowerSum = prefixSums[lowerIdx]\n                lowerEqual = query * (lowerIdx + 1)\n                deltaLower = abs(lowerEqual - lowerSum)\n\n            upperIdx = self.bSearchUpper(nums, query)\n            if(upperIdx != -1):\n                upperSum = prefixSumsRight[upperIdx]\n                upperEqual = query * (len(nums) - upperIdx)\n                deltaUpper = abs(upperEqual - upperSum)\n\n            curProblem = deltaLower + deltaUpper\n            answer[index] = curProblem\n        return answer\n\n    def bSearchLower(self, nums: List[int], query:int) -> int:\n        targetIdx = -1\n        low = 0\n        high = len(nums) - 1\n        while(low <= high):\n            mid = floor((low + high) * 0.5)\n            val = nums[mid]\n            if(val <= query):\n                if(mid >= targetIdx):\n                    targetIdx = mid\n                low = mid + 1\n            elif(val > query):\n                high = mid - 1\n        return targetIdx\n    \n    def bSearchUpper(self, nums: List[int], query:int) -> int:\n        targetIdx = float(\'inf\')\n        low = 0\n        high = len(nums) - 1\n        while(low <= high):\n            mid = floor((low + high) * 0.5)\n            val = nums[mid]\n            if(val > query):\n                if(mid <= targetIdx):\n                    targetIdx = mid\n                high = mid - 1\n            elif(val <= query):\n                low = mid + 1\n        if(targetIdx == float(\'inf\')):\n            targetIdx = -1\n        return targetIdx\n        \n\n    \n\n        \n```
['Array', 'Binary Search', 'Sorting', 'Prefix Sum', 'Python3']