Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

# PriorityQueue

Problem Description

The PriorityQueue class provides the functionality of the heap data structure. It implements the Queue interface.

Unlike normal queues, priority queue elements are retrieved in sorted order.

Suppose, we want to retrieve elements in ascending order. In this case, the head of the priority queue will be the smallest element. Once this element is retrieved, the next smallest element will be the head of the queue.

It is important to note that the elements of a priority queue may not be sorted. However, elements are always retrieved in sorted order.

In order to create a priority queue, we must import the java.util.PriorityQueue package. Once we import the package, here is how we can create a priority queue in Java.

PriorityQueue<Integer> numbers = new PriorityQueue<>();

Here, we have created a priority queue without any arguments. In this case, the head of the priority queue is the smallest element of the queue. And elements are removed in ascending order from the queue.

However, we can customize the ordering of elements with the help of the Comparator interface. We will learn about that later in this series.

Methods:

The PriorityQueue class provides the implementation of all the methods present in the Queue interface.

• add() :  Inserts the specified element to the queue. If the queue is full, it throws an exception. Time Complexity: O(logN) where N denotes the number of elements present in the PriorityQueue.
• offer() - Inserts the specified element to the queue. If the queue is full, it returns false. Time Complexity: O(logN)
• poll() - returns and removes the head of the queue. Time Complexity: O(logN)
• peek() - return the head of the queue. Time Complexity: O(1)

You need to think of a data structure and implement it such that it can help you in answering the below-mentioned queries.

You are given Q queries, in each query you are given two integers x and y:

if x = 1 then insert the integer y to your data structure.
if x = 2 then print an integer denoting the minimum element currently present in your data-structure, if there are no elements present then print -1
if x = 3 then remove the minimum element currently present in your data structure, if there is no element present then don't do anything.

Problem Constraints

1 <= Q <= 105

1 <= x <= 3

1 <= y <= 1000

Input Format

The first line of input contains a single integer Q.

Next, Q lines each contain two integers x and y for that query.

Output Format

For each query in which x = 2 print its answer in separate lines.

Example Input

Input 1:

``` 6
2 -1
1 5
1 2
2 -1
3 -1
2 -1
```

Example Output

Output 1:

``` -1
2
5
```

Example Explanation

Explanation 1:

``` Query 1: x = 2 but as we don't have any element so we will print -1.
Query 2: x = 1 we will insert 5 to our data structure.
Query 3: x = 1 we will insert 2 to our data structure.
Query 4: x = 2 the minimum element is 2
Query 5: x = 3 we will remove the minimum element i.e 2
Query 6: x = 2 the minium element now is 5
```

Hints
• Complete Solution

## Discussion

Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College/University Name
Job Title
Job Title
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer