Quick actions

cmd+k|ctrl+k

Navigation

Languages

Kth Smallest Element using heap

Snippet info

Language

Cpp

Visibility

public

Author

amangoyal1727

Created

2021-01-11T01:57:01Z

Updated

2021-01-11T01:57:01Z

#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;

int main() {
    
    priority_queue<int> maxh; //Max-Heap declaration
    int n;
    cin>>n;
    
    int k;
    cin>>k;
    
    int A[n];
    for(int i =0 ; i<n ; i++){
        cin>>A[i];
    }
    
    for(int i =0 ; i<n ; i++){
        maxh.push(A[i]);
        if(maxh.size()>k){
            maxh.pop();
        }
        
    }
    
   cout<<maxh.top();
    
    
    
}
INFO