K Largest elements using heap

Run Settings
LanguageC++
Language Version
Run Command
#include <iostream> #include <queue> #include <algorithm> using namespace std; int main() { int k; cin>>k; int n; cin>>n; int A[n]; for(int i =0; i<n ; i++){ cin>>A[i]; } priority_queue<int, vector<int>, greater<int>> minh; for(int i=0 ;i<n; i++){ minh.push(A[i]); if(minh.size()>k){ minh.pop(); } } cout<<minh.top()<<" "; minh.pop(); cout<<minh.top()<<" "; minh.pop(); cout<<minh.top()<<" "; }
Editor Settings
Theme
Key bindings
Full width
Lines