Quick actions

cmd+k|ctrl+k

Navigation

Languages

Top K Frequent Numbers

Snippet info

Language

Cpp

Visibility

public

Author

amangoyal1727

Created

2021-01-11T20:53:27Z

Updated

2021-01-11T20:53:27Z

#include <iostream>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
typedef pair<int , int > ppi; 
int main() {
    
    int k;
    cin>>k;
    
    int n;
    cin>>n;
    
    int A[n];
    for(int i=0; i<n ; i++){
        cin>>A[i];
    }
    
    map<int ,int> mp;
    
    for(int i=0 ; i<n ; i++){
        mp[A[i]]++;
    }
    
    priority_queue<ppi, vector<ppi>, greater<ppi>> minh;
    
    for(auto i = mp.begin(); i!=mp.end() ; i++){
        minh.push({i->second , i->first});
        if(minh.size()>k)
            minh.pop();
    }
    
    while(k>0){
        cout<<minh.top().second<<" ";
        minh.pop();
        k--;
    }
    
    
}
INFO