Kth Smallest Element using heap

Run Settings
LanguageC++
Language Version
Run Command
#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(); }
Editor Settings
Theme
Key bindings
Full width
Lines