-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind_Majority_Elements.cpp
More file actions
52 lines (49 loc) · 1.12 KB
/
Copy pathFind_Majority_Elements.cpp
File metadata and controls
52 lines (49 loc) · 1.12 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
/*
* @Date : 2020-04-22 16:02:56
* @Author : Abhimanyu Kumar Maurya (aerma7309@gmail.com)
*/
#include <bits/stdc++.h>
using namespace std;
bool ib = ios_base::sync_with_stdio(0);
bool it = cin.tie(0);
signed main()
{
int size, num;
cin >> size;
vector<int> v(size);
for (auto &i : v)
cin >> i;
int element1 = 0, element2 = 0, count1 = 0, count2 = 0;
for (auto &i : v)
{
if (i == element1)
++count1;
else if (i == element2)
++count2;
else if (count1 == 0)
element1 = i, count1 = 1;
else if (count2 == 0)
element2 = i, count2 = 1;
else
--count1, --count2;
}
count1 = 0, count2 = 0;
for (auto &i : v)
{
if (i == element1)
++count1;
else if (i == element2)
++count2;
}
if (count1 > size / 3 or count2 > size / 3)
{
if (count1 > (size / 3))
cout << element1 << " ";
if (count2 > (size / 3))
cout << element2 << " ";
}
else
cout << "No Majority Elements";
cout << '\n';
return 0;
}