2

Implement a priority queue

Difficulty: MEDIUM

Problem Statement
Suggest Edit

Ninja is given a task to implement a priority queue using Heap data structure. The Ninja is busying preparing for the tournament., So he asked for your help.

Your task is to use the class as shown in the comments in the code editor and complete the functions push(), pop(), getMaxElement() and isEmpty() to implement a priority queue.

Note:

There are four types of queries.  

Type 1: push( x ) push element ‘x’ into queue.

Type 2: pop( ) delete the largest element from the queue, if the queue is empty then do nothing.

Type 3: getMaxElement( ) return largest element , if queue is empty return -1;

Type 4: isEmpty( ) return ‘1’ if queue is empty, otherwise return ‘0’.
Input Format:
First-line will contain ‘Q’, the number of queries. Then the queries follow.

Each query has at least one integer representing the type of query. In the case of type 1, you have space-separated integer X.
Output Format:
For each query of type 3 output an integer value in the newline.
Note:
You have to complete the function only.
Constraints:
1 <= Q <= 100
1 <= X <= 10 ^ 9

Time limit: 1 sec.
Sample Input 1:
4
1 2
1 1
3
4
Sample Output 1:
2
False
Explanation:
After query 1, queue = {2}
After query 2, queue = {2, 1}.
After query 3, output the maximum number from queue i.e 2.
After query 4, Since queue= { 2 }, not empty, output “False”.
Sample Input 2:
5
1 2
2
3
1 5
3
Sample Output 2:
-1
5
Explanation :
After query 1, queue = {2}
After query 2, queue = {  }
After query 3, Since the queue is empty, the output is -1.
After query 4, queue = { 5 }
After query 5, output the maximum number from queue i.e 5
Reset Code
Full screen
copy-code
Console