VOOZH about

URL: https://www.geeksforgeeks.org/dsa/kth-largest-element-in-a-stream/

⇱ Kth largest in a stream - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Kth largest in a stream

Last Updated : 22 Apr, 2026

Given an input stream of n integers, represented as an array arr[], and an integer k. After each insertion of an element into the stream, you need to determine the kth largest element so far (considering all elements including duplicates). If k elements have not yet been inserted, return -1 for that insertion. Return an array of size n containing the kth largest element after each insertion step.

Examples: 

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.

[Naive Approach] Using Repeated Sorting - O(n*n*log(n)) Time and O(n) Space

The idea is to maintain a sorted array of all elements seen so far to easily access the kth smallest. We sort the array repeatedly after each insertion so that the elements are always in increasing order. This ensures that we can directly pick the kth smallest from a fixed index when we have at least k elements. If fewer than k elements are present, we return -1 since the kth smallest doesn't exist yet.

Dry run for arr[] = [1, 2, 3, 4, 5, 6], k = 4 :

  • i=0, topK = [1], size < k so res = [-1]
  • i=1, topK = [1,2], size < k so res = [-1,-1]
  • i=2, topK = [1,2,3], size < k so res = [-1,-1,-1]
  • i=3, topK = [1,2,3,4], pick topK[0] = 1 so res = [-1,-1,-1,1]
  • i=4, topK = [1,2,3,4,5], pick topK[1] = 2 so res = [-1,-1,-1,1,2]
  • i=5, topK = [1,2,3,4,5,6], pick topK[2] = 3 so res = [-1,-1,-1,1,2,3]

Final Output: [-1, -1, -1, 1, 2, 3]


Output
-1 -1 -1 1 2 3 

[Expected Approach] Using Min Heap - O(n*log(k)) Time and O(k) Space

The idea is to use Min Heap 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.

Dry run for arr[] = [1, 2, 3, 4, 5, 6], k = 4 :

  • i=0, heap = [1], size < k so res = [-1]
  • i=1, heap = [1,2], size < k so res = [-1,-1]
  • i=2, heap = [1,2,3], size < k so res = [-1,-1,-1]
  • i=3, heap = [1,2,3,4], kth = 1 so res = [-1,-1,-1,1]
  • i=4, pop(1), push(5), heap = [2,3,4,5], kth = 2 so res = [-1,-1,-1,1,2]
  • i=5, pop(2), push(6), heap = [3,4,5,6], kth = 3 so res = [-1,-1,-1,1,2,3]

Final Output: [-1, -1, -1, 1, 2, 3]


Output
-1 -1 -1 1 2 3 
Comment