forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Top_K_frequent_elements.cpp
67 lines (53 loc) · 1.4 KB
/
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
/*
Top K frequent Elements
======================================
Given a non-empty array of integers, return the k most frequent elements.
Link: https://leetcode.com/problems/top-k-frequent-elements/
Steps:
1.We will build a map and store frequency of every element in the array.
2.We will push all the elements of map along with their frequencies in a maxHeap as a pair.
3.Now, elements are sorted in descending order based on the first element of the pair.
4.Therefore, we will make the pair as {frequency of the element, element}.
5.We will run a loop till k is greater than 0 and maxHeap is not empty, pop the elements and print them.
You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
*/
#include <bits/stdc++.h>
using namespace std;
void topKFrequent(vector<int>& nums, int k) {
map<int,int>m;
priority_queue<pair<int,int>> pq;
for(int i: nums){
m[i]++;
}
for(auto i : m){
pq.push({i.second, i.first});
}
while(k-- && !pq.empty()){
cout<<pq.top().second<<" ";
pq.pop();
}
}
// Driver code
int main() {
// Take input
int n;
cin >> n;
vector<int> nums(n);
for(int i = 0; i<n; i++){
cin >> nums[i];
}
int k;
cin >> k;
topKFrequent(nums, k);
}
/*
Time Complexity: O(nlogn)
Space Complexity: O(n)
Input:
n = 8
nums = [1,1,7,4,2,2,2,3]
k = 2
Output: [2,1]
Explanation:
Top 2 frequent elements are 2 and 1 as 2 occurs 3 times and 1 occurs 2 times.
*/