Question
Consider the following Java code snippet: Â Â
import java.util.PriorityQueue;   public class HeapQuestion8 {     public static void main(String[] args) {       PriorityQueue pq = new PriorityQueue();       pq.add(5);       pq.add(1);       pq.add(5); // Adding a duplicate       pq.add(3);       System.out.println(pq.poll());       System.out.println(pq.poll());       System.out.println(pq.poll());     }   }   What will be the output of this program?Solution
A PriorityQueue (min-heap by default for Integers) correctly handles duplicate elements. They are treated like any other element based on their priority.   1. pq.add(5); pq.add(1); pq.add(5); pq.add(3);     The elements in the heap are {1, 3, 5, 5} (conceptually, 1 is at the root).   2. System.out.println(pq.poll());     Removes and prints the smallest element: 1. The heap now contains {3, 5, 5}.   3. System.out.println(pq.poll());     Removes and prints the next smallest element: 3. The heap now contains {5, 5}.   4. System.out.println(pq.poll());     Removes and prints the next smallest element: 5. The heap now contains {5}.   Therefore, the output is 1, 3, 5.
Arjun and Neha started a firm with investments of Rs.12000 and Rs.9000 respectively. Arjun invested for 8 months more than Neha. If the profit share of...
A and B enter into a partnership with their initial sum of Rs.35000 and Rs.45000 respectively. After 6 months, a third person C also joins them with his...
A started a business with an investment of Rs.1000. After some months, B joins the business with an investment of Rs.2000 and after three more months C ...
Radha started a dairy farm by investing Rs. 80,000. After 4 months, Krishna joined her, contributing Rs. 1,20,000 to the investment. At the end of one y...
A and S commenced a business with capitals of Rs. 15,000 and Rs. 13,500, respectively. After 9 months, they withdrew one third of their capital. Determi...
P and Q started a business by investing Rs.5600 and Rs.4000 respectively. After 6 months, Q increased his investment by a certain percentage such that a...
'A' and 'B' started a business together by investing Rs. (x + 4000) and Rs. (x + 2000). After 4 months, 'A' increased his investment by 50% while 'B' i...
A invested Rs. ‘x’ for 3 months, then added Rs. 400 for the next five months and exited. B invested Rs. ‘x’ for the remaining 4 months of the ye...
A started a business with an investment of Rs.1400. After some months, B joins the business with an investment of Rs.2400 and after two more months C jo...
"Anuj and Bishnu initiated a Construction business with Anuj investing Rs. 30,000 and Bishnu investing _________Â a certain amount of money. They hired...