You will be given a stream of numbers, and you need to find the 'kth' largest number in the stream at any given time.
As the stream of numbers can not be given during compile time, so you need to design a data structure which can accept infinite numbers and can return the 'kth' largest number at any given time.
The stream of numbers is nothing but a large collection of numbers from which integers are read at runtime, such as the user will never know the upper limit on the number of integers that will be read.
The implemented data structure must support the following operations:
1. add(DATA) :
This function should take one argument of type and store it in its pool and returns the 'kth' largest number from the current pool of integers.
You will be given 'q' queries:
val - For this query, insert the integer into your current pool of integers and return the 'kth' largest integer from the existing 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 equals k.
3. The maximum number of queries 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.
The first line contains two space-separated integers, 'Q’ and ‘K’, where 'Q' denotes the number of queries running 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 an integer ‘DATA’, 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 each query, prints the 'kth' largest integer from the current pool.
The output of each query 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.
2 3
2 1 3
3
5
2
3
The initial pool is - 2 1 3.
When 3 is added, the pool is now - 3 2 1 3. The 3rd largest element is 2(when we sort the pool, it becomes 3 3 2 1).
When 5 is added, the pool is now - 5 3 2 1 3. Even now, the 3rd largest element is 3(when we sort the pool, it becomes 5 3 3 2 1).
1 5
4 4 4 4 2
3
3
1 <= Q <= 10 ^ 4
1 <= K <= 10 ^ 5
1 <= DATA <= 10 ^ 9
Time Limit: 1 sec.
Try maintaining the sorted order of the current pool.
O((N ^ 2) * log(N)), where ‘N’ is the maximum number of integers we are reading at run time.
The best sorting algorithm sorts the array of size N in N * log(N) time, so we are sorting after each addition.
O(N), where ‘N’ is the maximum number of integers we are reading at run time.
An auxiliary space is used to store the elements of the array. Therefore, overall space complexity will be O(N)