forked from krish-ag/HacktoberFest22-Repo-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Majority Element II.cpp
46 lines (40 loc) · 937 Bytes
/
Majority Element II.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
vector<int> res;
void majorityElement(vector<int>& nums) {
int n1=-1, n2=-1, n=nums.size();
int c1=0, c2=0;
for(auto ele:nums){
if(n1==ele) c1++;
else if(n2==ele) c2++;
else if(c1==0){
n1=ele;
c1=1;
}
else if(c2==0){
n2=ele;
c2=1;
}
else{
c1--;
c2--;
}
}
c1=0,c2=0;
for(int i=0;i<n;i++){
if(nums[i]==n1) c1++;
else if(nums[i]==n2) c2++;
}
if(c1>n/3)
res.push_back(n1);
if(c2>n/3)
res.push_back(n2);
}
int main(){
int n;
cin >> n;
vector<int> nums(n, 0);
for(int i=0;i<n;i++)
cin >> nums[i];
majorityElement(nums);
for(int i=0;i<res.size();i++)
cout << res[i] << " ";
}