Complete the code to initialize the max heap for the lower half of numbers.
PriorityQueue<Integer> maxHeap = new PriorityQueue<>([1]);The max heap is created by providing a comparator that reverses the natural order, so the largest element is at the top.
Complete the code to add a number to the max heap.
maxHeap.[1](num);poll or pop which remove elements instead of adding.peek which only returns the top element without removing or adding.The offer method adds an element to the PriorityQueue.
Fix the error in balancing heaps by completing the condition to check if max heap size is greater than min heap size by more than one.
if (maxHeap.size() [1] minHeap.size() + 1) {
We check if max heap size is greater than min heap size by more than one to rebalance.
Fill both blanks to complete the code that moves the top element from max heap to min heap.
minHeap.[1](maxHeap.[2]());
peek which does not remove the element.add instead of offer which is preferred for PriorityQueue.We remove the top element from max heap using poll and add it to min heap using offer.
Fill all three blanks to complete the code that returns the median from the two heaps.
if (maxHeap.size() == minHeap.size()) { return (maxHeap.[1]() + minHeap.[2]()) / 2.0; } else { return maxHeap.[3](); }
poll which removes elements and changes heap state.size which returns the number of elements, not the top element.We use peek to look at the top elements without removing them to calculate the median.
