Kth largest element after every insertion
Last Updated :
29 Aug, 2022
Given an infinite stream of integers, find the k’th largest element at any point of time. It may be assumed that 1 <= k <= n.
Input:
stream[] = {10, 20, 11, 70, 50, 40, 100, 5, ...}
k = 3
Output: {_, _, 10, 11, 20, 40, 50, 50, ...}
Extra space allowed is O(k).
The idea is to use min heap.
- Store first k elements in min heap.
- For every element from (k+1)-th to n-th, do following.
- Print root of heap.
- If current element is more than root of heap, pop root and insert
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int kthLargest( int stream[], int n, int k)
{
priority_queue< int , vector< int >, greater< int > > pq;
for ( int i=0; i<k-1; i++)
{
pq.push(stream[i]);
cout << "_ " ;
}
pq.push(stream[k-1]);
for ( int i=k; i<n; i++)
{
cout << pq.top() << " " ;
if (stream[i] > pq.top())
{
pq.pop();
pq.push(stream[i]);
}
}
cout << pq.top();
}
int main()
{
int arr[] = {10, 20, 11, 70, 50, 40, 100, 55};
int k = 3;
int n = sizeof (arr)/ sizeof (arr[0]);
kthLargest(arr, n, k);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void kthLargest( int stream[], int n, int k)
{
Vector<Integer> pq = new Vector<Integer>(n);
for ( int i = 0 ; i < k - 1 ; i++)
{
pq.add(stream[i]);
System.out.print( "_ " );
}
pq.add(stream[k - 1 ]);
for ( int i = k; i < n; i++)
{
Collections.sort(pq);
System.out.print(pq.get( 0 ) + " " );
if (stream[i] > pq.get( 0 ))
{
pq.remove( 0 );
pq.add(stream[i]);
}
}
Collections.sort(pq);
System.out.print(pq.get( 0 ));
}
public static void main(String[] args) {
int arr[] = { 10 , 20 , 11 , 70 , 50 , 40 , 100 , 55 };
int k = 3 ;
int n = arr.length;
kthLargest(arr, n, k);
}
}
|
Python3
import heapq
def kthLargest(stream, n, k):
pq = []
for i in range (k - 1 ):
pq.append(stream[i])
print ( "_ " , end = "")
pq.append(stream[k - 1 ])
heapq.heapify(pq)
for i in range (k,n):
print (pq[ 0 ],end = " " )
if (stream[i]>pq[ 0 ]):
pq[ 0 ] = pq[ - 1 ]
pq.pop()
pq.append(stream[i])
heapq.heapify(pq);
print (pq[ 0 ])
arr = [ 10 , 20 , 11 , 70 , 50 , 40 , 100 , 55 ]
k = 3
n = len (arr)
kthLargest(arr, n, k)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void kthLargest( int [] stream, int n, int k)
{
List< int > pq = new List< int >();
for ( int i = 0; i < k - 1; i++)
{
pq.Add(stream[i]);
Console.Write( "_ " );
}
pq.Add(stream[k - 1]);
for ( int i = k; i < n; i++)
{
pq.Sort();
Console.Write(pq[0] + " " );
if (stream[i] > pq[0])
{
pq.RemoveAt(0);
pq.Add(stream[i]);
}
}
pq.Sort();
Console.Write(pq[0]);
}
static void Main()
{
int [] arr = {10, 20, 11, 70, 50, 40, 100, 55};
int k = 3;
int n = arr.Length;
kthLargest(arr, n, k);
}
}
|
Javascript
<script>
function kthLargest(stream, n, k) {
let pq = new Array();
for (let i = 0; i < k - 1; i++) {
pq.push(stream[i]);
document.write( "_ " );
}
pq.push(stream[k - 1]);
for (let i = k; i < n; i++) {
pq.sort((a, b) => a - b);
document.write(pq[0] + " " );
if (stream[i] > pq[0]) {
pq.shift(0);
pq.push(stream[i]);
}
}
pq.sort((a, b) => a - b);
document.write(pq[0]);
}
let arr = [10, 20, 11, 70, 50, 40, 100, 55];
let k = 3;
let n = arr.length;
kthLargest(arr, n, k);
</script>
|
Output
_ _ 10 11 20 40 50 55
If stream contains elements of non-primitive types, we may define our own compactor function and create a priority_queue accordingly.
Share your thoughts in the comments
Please Login to comment...