Kth largest element in a stream | Practice | GeeksforGeeks

Given an input stream arr[] of n integers. Find the Kth largest element for each element in the stream and if the Kth element doesn't exist, return -1.
Example 1:
Input:
k = 4, n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output:
-1 -1 -1 1 2 3
Explanation:
k = 4
For 1, the 4th largest element doesn't
exist so we print -1.
For 2, the 4th largest element doesn't
exist so we print -1.
For 3, the 4th largest element doesn't
exist so we print -1.
For 4, the 4th largest element is 1.
For 5, the 4th largest element is 2.
for 6, the 4th largest element is 3.
Example 2:
Input:
k = 1, n = 2
arr[] = {3, 4}
Output:
3 4 
Explanation: 
For the 1st and 2nd element the 1st largest 
element is itself.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function kthLargest() which takes 2 Integers k, and n and also an array arr[] of size n as input and returns the kth largest element in the stream.
 
Expected Time Complexity: O(nlogk)
Expected Auxiliary Space: O(n)
 
Constraints:
1 ≤ k ≤ n ≤ 105
1 ≤ arr[i] ≤ 105
0
rayalravi20016 days ago
c++  solution
 
 vector<int> res;       int old=0;       priority_queue<int,vector<int>,greater<int>> pq;       for(int i=0; i<n; i++){           if(i<k-1){              res.push_back(-1);              pq.push(arr[i]);            }           else{               pq.push(arr[i]);               int top = pq.top();               (old>top)?res.push_back(old):res.push_back(top);               if(old<top){                   old = top;               }               pq.pop();           }        }       return res;
+1
bhaskarmaheshwari84 weeks ago
int prev=0;       vector<int> v(n,-1);       priority_queue<int,vector<int>,greater<int>> q;       for(int i=0;i<n;i++)       {           q.push(arr[i]);           if(i+1>=k)           {               if(q.top()>prev)               {                   v[i]=q.top();                   prev=q.top();                   q.pop();               }               else               {                   v[i]=prev;                   q.pop();               }                                                        }       }       return v;
+1
badgujarsachin832 months ago
vector<int> kthLargest(int k, int arr[], int n) {
        // code here
        priority_queue<int,vector<int>,greater<int>> q;
        vector<int > v;
        for(int i=0;i<k;i++){
            q.push(arr[i]);
            if(i<k-1){
                v.push_back(-1);
            }
        }
        for(int i=k;i<n;i++){
            v.push_back(q.top());
            if(arr[i]>q.top()){
                q.pop();
                q.push(arr[i]);
            }
        }
        v.push_back(q.top());
        return v;
    }
-1
saurabhsathe12343 months ago
Simple C++ Solution Using MinHeap : 0.7sec
vector<int> kthLargest(int k, int arr[], int n) {
        
        // code here
        priority_queue<int , vector<int>,greater<int>> pq;
        vector<int> v;
        for(int i=0;i<k;i++)
        {
            pq.push(arr[i]);
            if(i<k-1)
                v.push_back(-1);
        }
        for(int i=k ; i<n ; i++)
        {
            v.push_back(pq.top());
            if(arr[i]> pq.top())
            {
                pq.pop();
                pq.push(arr[i]);
                
            }
        }
        v.push_back(pq.top());
        return v;
    }
+1
abhixhek053 months ago
  vector<int> kthLargest(int k, int arr[], int n) {
        // code here
        priority_queue<int,vector<int>, greater<int>>q;
        vector<int>ans;
        for(int i=0;i<k-1;i++){
                ans.push_back(-1);
                q.push(arr[i]);
                
        }
        for(int i=k-1;i<n;i++){
             q.push(arr[i]);
           
            if(q.size()>k){
                q.pop();
            }
            ans.push_back(q.top()); 
        }
        return ans;
    }
-2
kumaarsahab4323 months ago
//Using Priority Queue
vector<int> kthLargest(int k, int arr[], int n) {
        vector<int> ans ;
        priority_queue<int, vector<int>, greater<int>> pq ;
        for(int i=0 ; i<n ; i++)
        {
            pq.push(arr[i]) ;
            if(pq.size()>k) pq.pop() ;
            if(pq.size()==k)
                ans.push_back(pq.top()) ;
            else ans.push_back(-1) ;
        }
        
        return ans ;
    }
-1
areeshaanjum7483 months ago
class Solution {   static int[] kthLargest(int k, int[] arr, int n) {              int[] ans = new int[n];       int i = 0; //i will point to ans array              //We will add (-1) k-1 times bcoz upto that       //kth element would not exist              while(i < k-1){           ans[i] = -1;           i++;       }              //Let us take the stream as 2, 1, 4, 3       //4th largest elem will be '1'              PriorityQueue<Integer> minHeap = new PriorityQueue<>(k);       int index = 0;       while(index < k){           minHeap.add(arr[index]);           index++;       }       ans[i] = minHeap.peek();       i++;              //Now, the stream becomes 2, 1, 4, 3, 0       //this '0' will not change 4th largest elem       //So we can say any elem less than the 4th largest elem       //will not change the 4th largest elem              //Atlast, our stream becomes 2, 1, 4, 3, 0, 5       //this '5' will definitely change the 4th largest elem       //now our 4th largest elem will be '2'              while(index<n){           if(arr[index] > minHeap.peek()){               minHeap.poll();               minHeap.add(arr[index]);           }                      ans[i] = minHeap.peek();           index++;           i++;       }              return ans;   }};
-3
harshchittora20013 months ago
 vector<int> kthLargest(int k, int arr[], int n) {       // code here       vector<int>x;       priority_queue<int,vector<int>,greater<int>>min_heap;       for(int i=0;i<n;i++)       {       min_heap.push(arr[i]);       if(min_heap.size()>k)       {           min_heap.pop();       }       if(min_heap.size()>=k)       {           x.push_back(min_heap.top());       }       else if(min_heap.size()<=k)       {           x.push_back(-1);       }       }       return x;   }
-1
nitind3563 months ago
Simple C++ Code:-

vector<int> kthLargest(int k, int arr[], int n) {
     vector<int>x;
     priority_queue<int,vector<int>,greater<int>>min;
     for(int i=0;i<n;i++)
     {
         min.push(arr[i]);
         if(min.size()>k)
         {
            min.pop();
         }
         if(min.size()>=k)
         {
            x.push_back(min.top());
         }
         else if(min.size()<k)
         {
            x.push_back(-1);
         }
     }
     return x;
}
-2
radheshyamnitj4 months ago
 vector<int>ans;            priority_queue<int,vector<int>,greater<int>> minheap;
        
        for(int i=0;i<n;i++){
            
            if(minheap.size()<k)
             minheap.push(arr[i]);
             
             if(minheap.size()<k)
               ans.push_back(-1);
             
             if(minheap.size()==k) {
                ans.push_back(minheap.top());
                
                if(minheap.top() < arr[i+1] && i<n-1)
                  minheap.pop();
            }  
        }
        return ans;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.