π One-stop destination for all your technical interview Preparation π
You are given an Integer array βARRβ and an Integer βKβ. Your task is to find the βKβ most frequent elements in βARRβ. Return the elements sorted in ascending order.
#include <bits/stdc++.h>
vector<int> KMostFrequent(int n, int k, vector<int>& arr)
{
map<int, int> mp;
for (int i = 0; i < n; i++) {
mp[arr[i]]++;
}
vector<int> ans;
priority_queue<pair<int, int>> pq;
for (auto it : mp) {
pq.push({it.second, it.first});
}
for (int i = 0; i < k; i++) {
ans.push_back(pq.top().second);
pq.pop();
}
sort(ans.begin(), ans.end());
return ans;
}