# Kth Largest Number

Posted: 26 Oct, 2020
Difficulty: Moderate

## PROBLEM STATEMENT

#### The implemented data structure must support the following operations:

``````1. add(DATA) :
This function should take one argument of type integer
and store it in its pool.
2. int getKthLargest() :
This function should return the kth largest number from
the current pool of integers.
``````

#### You will be given q queries of 2 types:

``````1. 1 val - For this type of query, you need to insert the integer into your current pool of integers
2. 2 - For this type of query, you need to return the kth largest integer from the current pool of integers.
``````
##### Note
`````` 1. The maximum number of integers that will be given will always be under memory limits.
2. You will also be given an initial pool of integers whose size will be equal to k.
3. The maximum queries of type 1 will be less than 10^5.
4. The kth largest element is not the kth distinct element but the kth largest element in the sorted order.
5. There will be at least one query of type 2.
``````
##### Input Format:
``````The first line contains two space-separated integers 'Q’ and ‘K’,  where Q denotes the number of queries which will be run against the implemented data structure.

The second line will contain ‘K’ space-separated integers which will be the initial pool of integers.

Then Q lines follow. The i-th line contains the i-th query in the format as in the problem statement

For the query of the first type, the input line will contain two integers ‘QUERYTYPE’ and ‘DATA’ separated by a single space, representing the type of the operation in integer and the integer data to be included in the pool respectively.

For the rest of the queries, the input line will contain only one integer value, representing the query being performed.
``````
##### Output Format:
``````For Query-1, you do not need to return anything.

For Query-2, prints the kth largest integer from the current pool.

The output of each query of type 2 has to be printed in a separate line.
``````
##### Note:
``````You do not need to print anything, it has already been taken care of. Just implement the given functions.
``````
##### Constraints:
``````1 <= Q <= 10 ^ 4
1 <= K <= 10 ^ 5
1 <= QUERYTYPE <= 2
1 <= DATA <= 10 ^ 9

Time Limit: 1 sec.
`````` Approach 1
• The brute force approach for this problem is very trivial.
• We can always maintain an array that will hold the integers, sort the array in non-increasing order and return the element present at the kth index(1-based indexing).
• The array should be such that it can add elements efficiently, for example, vectors in C++, lists in python, etc. Whenever we receive a query of type 1, we add the element in the array and sort it.
• For sorting, we can use the inbuilt sort function in many programming languages such as C++, Java, etc.