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.
What is the ratio whose terms differ by 45 and the measure of which is 4/9?
The number of students in class IX and class X is 70 and 75, respectively. The ratio of the number of boys to girls in classes IX and X is 9: 5 and 8: 7...
If the third proportion of 30 and 45 is “0.5k”, then find the value of (k − 18) : (k + 22).
An amount is distributed among M, N, O, and P in the ratio 10:6:8:5 respectively. If the amount received by O is Rs. 1,600, then find the amount receive...
A stone worth Rs.5000 drops and breaks with weight in the ratio 1:3:6. The price of stone is directly proportional to square of its weight. Find the tot...
The raw material cost of an article is 1/3 of its manufacture cost. If the cost of manufacturing is decreased in the ratio of 4 : 3 and the cost of its ...
The ratio of the ages of Sneha, Ritu, and Anjali is 3:4:5 respectively. Meera is 6 years younger than Ritu. If the difference between Anjali and Sneha�...
The total amount of money with three people is Rs 10500. If the ratio of amount with first person and second person is 7:3 and the ratio of amount with ...
Divide Rs 1,200 between A and B in the ratio 3 : 5.
The ratio of the monthly salary of Supriya to that of Mohini is 3 : 5. Supriya and Mohini both save 30% and 42% respectively of their monthly salaries r...