Not being able to fix the error

#include
#include
#include
#include
using namespace std;
bool compare(pair<int,int> a,pair<int,int> b)
{ if(a.second==b.second)
return a.first>b.first;
return a.second>b.second;
}
void find(int a[],int n,int k)
{
map<int,int>m;
for(int i=0;i<n;i++)
m[a[i]]++;

vector<pair<int,int>>freqv(m.begin(),m.end());

priority_queue<pair<int, int>, vector<pair<int, int>>,compare>pq(m.begin(), m.end());

for (int i = 0; i < k; i++) 
{ 
    cout <<pq.top().first<<" "; 
    pq.pop(); 
}

}
int main()
{
int n,k;
cin>>n;
cin>>k;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
find(a,n,k);
return 0;
}

please share the link of code

paste you code at

click on file->save->link will be generated
send the link of code generated
it will look like https://ide.codingblocks.com/s/258793

this is not correct way to use comparator for map

correct one is

class compare { 
public: 
    bool operator()(pair<int, int> p1, pair<int, int> p2) 
    { 
        if (p1.second == p2.second) return p1.first > p2.first; 

        return p1.second < p2.second; 
    } 
}; 

Modified Code

i hope this helps
if yes hit a like and don’t forgot to mark doubt as resolved
if you have more doubts regarding this feel free to ask

1 Like

I submited the same code but but its not passing any of the test cases.
Although when i am compiling and checking most of the test cases are working

share the link of Final Code


Here’s the link

actually in comparator correct condition is

if (p1.second == p2.second) return p1.first > p2.first;

now try

Still getting it wrong for all the testcases.

can you send me the link of problem form hacker blocks
so that i can check it by running

because your doubt is missing form my feed so i am not able to see the question

I cant find it on hacker block. But i can tell you the question name its K most frequent elements.


and this is my code

Hello ?? Is anyone there ?? Respond to my problem.

sorry i missed your doubt
let me see

problem is with setter
i will update you about this soon