You are given an array nums of positive integers. In one operation, you can choose any number from nums and reduce it to exactly half the number. (Note that you may choose this reduced number in future operations.)

Return the minimum number of operations to reduce the sum of nums by at least half.

&nbsp;
Example 1:

Input: nums = [5,19,8,1]
Output: 3
Explanation: The initial sum of nums is equal to 5 + 19 + 8 + 1 = 33.
The following is one of the ways to reduce the sum by at least half:
Pick the number 19 and reduce it to 9.5.
Pick the number 9.5 and reduce it to 4.75.
Pick the number 8 and reduce it to 4.
The final array is [5, 4.75, 4, 1] with a total sum of 5 + 4.75 + 4 + 1 = 14.75. 
The sum of nums has been reduced by 33 - 14.75 = 18.25, which is at least half of the initial sum, 18.25 &gt;= 33/2 = 16.5.
Overall, 3 operations were used so we return 3.
It can be shown that we cannot reduce the sum by at least half in less than 3 operations.


Example 2:

Input: nums = [3,8,20]
Output: 3
Explanation: The initial sum of nums is equal to 3 + 8 + 20 = 31.
The following is one of the ways to reduce the sum by at least half:
Pick the number 20 and reduce it to 10.
Pick the number 10 and reduce it to 5.
Pick the number 3 and reduce it to 1.5.
The final array is [1.5, 8, 5] with a total sum of 1.5 + 8 + 5 = 14.5. 
The sum of nums has been reduced by 31 - 14.5 = 16.5, which is at least half of the initial sum, 16.5 &gt;= 31/2 = 16.5.
Overall, 3 operations were used so we return 3.
It can be shown that we cannot reduce the sum by at least half in less than 3 operations.


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 105
	1 &lt;= nums[i] &lt;= 107


class Solution:
    def halveArray(self, nums: List[int]) -> int:
        # Creating empty heap
        maxHeap = []
        heapify(maxHeap) # Creates minHeap 
        
        totalSum = 0
        for i in nums:
            # Adding items to the heap using heappush
            # for maxHeap, function by multiplying them with -1
            heappush(maxHeap, -1*i) 
            totalSum += i
        
        requiredSum = totalSum / 2
        minOps = 0
        
        while totalSum > requiredSum:
            x = -1*heappop(maxHeap) # Got negative value make it positive
            x /= 2
            totalSum -= x
            heappush(maxHeap, -1*x) 
            minOps += 1
        
        return minOps
class Solution {
    public int halveArray(int[] nums) {
        PriorityQueue<Double> q = new PriorityQueue<>(Collections.reverseOrder());
        double sum=0;
        for(int i:nums){
            sum+=(double)i;
            q.add((double)i);
        }
        int res=0;
        double req = sum;
        while(sum > req/2){
            double curr = q.poll();
            q.add(curr/2);
            res++;
            sum -= curr/2;
        }
        return res;
    }
}
class Solution {
public:
    int halveArray(vector<int>& nums) {
		priority_queue<double> pq;
        double totalSum = 0;
        double requiredSum = 0;
        for(auto x: nums){
            totalSum += x;
            pq.push(x);
        }
        
        requiredSum = totalSum/2;
        int minOps = 0;
        while(totalSum > requiredSum){
            double currtop = pq.top();
            pq.pop();
            currtop = currtop/2;
            totalSum -= currtop;
            pq.push(currtop);
            minOps++;
        }
        return minOps;
	}
}
		
var halveArray = function(nums) {
    const n = nums.length;
    const maxHeap = new MaxPriorityQueue({ priority: x => x });

    let startSum = 0;

    for (const num of nums) {
        maxHeap.enqueue(num);
        startSum += num;
    }

    let currSum = startSum;

    let numberOfOperations = 0;

    while (currSum > startSum / 2) {
        const biggestNum = maxHeap.dequeue().element;

        const halfNum = biggestNum / 2;

        numberOfOperations += 1;
        currSum -= halfNum;

        maxHeap.enqueue(halfNum);
    }

    return numberOfOperations;
};
Minimum Operations to Halve Array Sum