-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmajorityElementII.cpp
More file actions
57 lines (52 loc) · 1.45 KB
/
majorityElementII.cpp
File metadata and controls
57 lines (52 loc) · 1.45 KB
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
// Majority Element - II
// You are given an array/list 'ARR' of integers of length ‘N’.
// You are supposed to find all the elements that occur strictly more than floor(N/3) times in the given array/list.
// Sample Input 1:
// 2
// 7
// 3 2 2 1 5 2 3
// 5
// 7 4 4 9 7
// Sample Output 1:
// 2
// 4 7
// Explanation
// In the first test case, floor(N/3) = floor(7/3)
// is equal to 2, and 2 occurs 3 times which is strictly more than N/3. No other element occurs more than 2 times.
// In the second test case, floor(N/3) = floor(5/3) is equal to 1,
// and 4 and 7 both occur 2 times. No other element occurs more than once.
// Sample Input 2:
// 2
// 6
// 1 2 4 4 3 4
// 4
// 6 6 6 7
// Sample Output 2:
// 4
// 6
// In the first test case, floor(N/3) = floor(6/3) is equal to 2, and 4 occurs 3 times
// which is strictly more than N/3. No other element occurs more than 2 times.
// In the second test case, floor(N/3) = floor(4/3) is equal to 1, and 6 occurs 3 times.
// No other element occurs more than once.
#include <bits/stdc++.h>
vector<int> majorityElementII(vector<int> &arr)
{
int n = arr.size();
int majority = floor(n/3);
vector<int> res;
unordered_map<int, int> m;
for(auto i: arr){
if(m.find(i) != m.end()){
m[i]++;
}
else{
m.insert(make_pair(i, 1));
}
}
for(auto i: m){
if(i.second > majority){
res.push_back(i.first);
}
}
return res;
}