Minimum Operations to Halve Array Sum LeetCode solution in Python
1class Solution:2def halveArray(self, nums: List[int]) -> int:3# Creating empty heap4maxHeap = []5heapify(maxHeap) # Creates minHeap67totalSum = 08for i in nums:9# Adding items to the heap using heappush10# for maxHeap, function by multiplying them with -111heappush(maxHeap, -1 * i)12totalSum += i1314requiredSum = totalSum / 215minOps = 01617while totalSum > requiredSum:18x = -1 * heappop(maxHeap) # Got negative value make it positive19x /= 220totalSum -= x21heappush(maxHeap, -1 * x)22minOps += 12324return minOps