Kth Largest Element In A Stream

easy
1. You have to define the class to find Kth largest element in a stream. 2. You have to complete the constructor of the class and the add function. 3. The constructor takes input K and an array of N elements, representing the initial elements of the stream. 4. The add function takes the next element from the stream and returns the element representing the Kth largest element at the moment. 5. For example, int K = 3; int[] arr = [4,5,8,2]; KthLargest obj = new KthLargest(3, arr); obj.add(3); // returns 4 obj.add(5); // returns 5 obj.add(10); // returns 5 obj.add(9); // returns 8 obj.add(4); // returns 8 6. main takes input from the users and creates the object of class KthLargest. 7. This is a functional problem. 8. You have to complete the constructor and add function of the class KthLargest. The signature is already defined for you. 9. Don't change the code of main.

Input Format

First line takes input N, the length of the array. Second line takes input N space separated numbers representing the elements of the array. Third line takes input K. Fourth line takes input M, representing the number of remaining elements in the stream. Fifth line takes M space separated integers representing the remaining elements of the stream. Input is handled for you.

Output Format

M lines of integers representing the Kth largest element, after adding each element of the stream. Output is handled for you.

Constraints

1 <= N <= 1000

Notice

Try First, Check Solution later

1. You should first read the question and watch the question video.
2. Think of a solution approach, then try and submit the question on editor tab.
3. We strongly advise you to watch the solution video for prescribed approach.

Example

Input
4
4 5 8 2
3
5
3 5 10 9 4
Output
4
5
5
8
8
Previous
Kth Largest Element In An Array
Next
Ladoo Problem

Related Questions