Input: arr[] = [1, 2, 3, 4, 5, 6], k = 4
Output: -1 -1 -1 1 2 3
Explanation: The first three insertions have fewer than 4 elements, so output is -1. From the fourth insertion onward, the kth largest elements are 1, 2, and 3 respectively.
Input: arr[] = [10, 20, 5, 15], k = 2
Output: -1 10 10 15
Explanation: First insertion gives -1 as fewer than 2 elements. After second, 2nd largest is 10. Then still 10. After inserting 15, elements are [10, 20, 5, 15]; 2nd largest is now 15.
Input: arr[] = [3, 4], k = 1
Output: 3 4
Explanation: After each insertion, there is at least 1 element, so the 1st largest elements are 3 and then 4.
The idea is to efficiently find the kth largest element in a stream using a Min Heap. The thought process is to maintain the k largest elements seen so far in the heap. Since the smallest among these k is the kth largest overall, it is always at the top of the heap. For each new element, if it's larger than the smallest in the heap, we replace the top element to keep only the largest k elements.
The idea is to maintain a set of the k largest elements seen so far similar to the previous approach. As we process each element, we insert it into the set, and if its size exceeds k, we remove the smallest. This ensures the smallest in the set is always the kth largest. We add -1 if fewer than k elements have been seen.