forked from msameerfarooq/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2. Top K frequent Elements.cpp
67 lines (60 loc) · 1.59 KB
/
2. Top K frequent Elements.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Given a non-empty array of integers, find the top k elements which have the highest frequency in the array. If two numbers have the same frequency then the larger number should be given preference.
Example 1:
Input:
nums = {1,1,1,2,2,3},
k = 2
Output: {1, 2}
Example 2:
Input:
nums = {1,1,2,2,3,3,3,4},
k = 2
Output: {3, 2}
Explanation: Elements 1 and 2 have the
same frequency ie. 2. Therefore, in this
case, the answer includes the element 2
as 2 > 1.
Expected Time Complexity : O(NlogN)
Expected Auxilliary Space : O(N)
Constraints:
1 <= N <= 10^5
1<=A[i]<=10^5
*/
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<int> topK(vector<int>& nums, int k) {
map<int,int> freq; //Storing the Frequency of each element in Hash Map
for(int i=0; i<nums.size(); ++i) {
freq[nums[i]]++;
}
vector<pair<int,int>> vec;
for(auto i: freq) {
vec.push_back({i.second,i.first});
}
// Storing {Freqency, Element} pair in vector and sorting descending order to take top k elements
sort(vec.rbegin(),vec.rend());
vector<int> ans;
int len=vec.size();
for(int i=0; i<min(k,len); ++i) ans.push_back(vec[i].second);
return ans;
}
};
int main() {
int tc;
cin >> tc;
while (tc--) {
int n;
cin >> n;
vector<int> nums(n);
for (auto &i : nums) cin >> i;
int k;
cin >> k;
Solution obj;
vector<int> ans = obj.topK(nums, k);
for (auto i : ans) cout << i << " ";
cout << "\n";
}
return 0;
}