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 Comparator

Problem Description

In the previous problem, priority queue elements are retrieved in the natural order (ascending order). However, we can customize this ordering.

For this, we need to create our own comparator class that implements the Comparator interface.

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 maximum element currently present in your data-structure, if there are no elements present then print -1
if x = 3 then remove the maximum 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

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
5
2```

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 maximum element is 2=5
Query 5: x = 3 we will remove the maximum element i.e 5
Query 6: x = 2 the maximum element now is 2
```

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 you graduated from
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