Question
Consider the following Java code snippet: Â Â
import java.util.Collections; Â Â import java.util.PriorityQueue; Â Â public class HeapQuestion2 { Â Â Â Â public static void main(String[] args) { Â Â Â Â Â Â PriorityQueue maxHeap = new PriorityQueue(Collections.reverseOrder()); Â Â Â Â Â Â maxHeap.add(10); Â Â Â Â Â Â maxHeap.add(5); Â Â Â Â Â Â maxHeap.add(20); Â Â Â Â Â Â maxHeap.add(3); Â Â Â Â Â Â System.out.println(maxHeap.poll()); Â Â Â Â Â Â System.out.println(maxHeap.poll()); Â Â Â Â } Â Â } Â Â What will be the output of this program?Solution
By passing Collections.reverseOrder() to the PriorityQueue constructor, we create a max-heap, where the largest element has the highest priority (is at the root).   1. maxHeap.add(10); maxHeap.add(5); maxHeap.add(20); maxHeap.add(3);     After these additions, the heap contains {20, 10, 5, 3} (conceptually, 20 is at the root).   2. System.out.println(maxHeap.poll());     poll() removes and returns the largest element. So, 20 is removed and printed. The heap now contains {10, 5, 3}.   3. System.out.println(maxHeap.poll());     poll() removes and returns the largest element again. So, 10 is removed and printed. The heap now contains {5, 3}.   Therefore, the output is 20, 10.
A shopkeeper sets the selling price of an item higher than its cost price. After offering a 4% discount, the shopkeeper earns a 20% profit. What percent...
An article with cost price of Rs. 300 is marked (50/3)% above its cost price and sold after a discount of 20%. Find the overall profit/loss in the trans...
Roshan purchased 100 rolls of paper for Rs. 20 per roll. He spent Rs. 400 on their transportation. He also paid 20 paise per roll for packing of each ro...
- Difference between selling price of certain number of items, when sold at Rs. 16 per item instead of Rs. 12 per item is Rs. 128. Total cost price of these ...
A train travelling at the speed of 72 km/hr crosses another train, having 20 meters less length and travelling in opposite direction at the speed of 54 ...
Average of first 5 numbers is 4 times the average of next 4 numbers. If the average of all 9 numbers is 36, find the sum of the last 4 numbers.
If ‘÷’ means ‘-’, ‘-’ mean ‘+’, ‘+’ means ‘×’ and ‘×’ means ‘÷’, compute the value of the expression: 126 × 14 – ...
Simplify the following expression: 125 + 12.5% of 960 - 15 x 4 - 24 x 3 + 20% of 175
If A:B = 5:8 and C:B = 4:9 then find the value of (A:B:C) .
If person 'A' can complete a task in 35 hours and person 'B' can complete the same task in 40 hours, how much time will it take for both 'A' and 'B' to ...