0

Top k frequent numbers

Program to find top k frequent numbers using heap I try but my ans is not correct so plzz help me

2nd Jun 2021, 1:54 PM
Avinash Giri
Avinash Giri - avatar
1 Respuesta
0
#include<iostream> using namespace std; #include<queue> #include<unordered_map> void top_freq(int arr[],int k,int size) { unordered_map<int,int>mp; priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>MIN; for(int i=0;i<size;i++) { mp[arr[i]]++; } for(auto i= mp.begin();i!=mp.end();i++) { MIN.push({i->first,i->second}); if(MIN.size()>k) { MIN.pop(); } while(MIN.size()>0) { cout<<MIN.top().second<<endl; MIN.pop(); } } } int main() { int arr[]={1,1,1,3,2,2,4}; int k=3; int size=sizeof(arr)/sizeof(arr[0]); top_freq(arr,k,size); }
2nd Jun 2021, 1:54 PM
Avinash Giri
Avinash Giri - avatar