-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathMoore's Voting Algorithm.cpp
More file actions
56 lines (52 loc) · 1.07 KB
/
Moore's Voting Algorithm.cpp
File metadata and controls
56 lines (52 loc) · 1.07 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
/* Algorithm to find majority element in an array if exist.
Majority element in an array of size n is an element that appear more than (n / 2) times
and hence there is atmost one such element.
Time Complexity: O(n)
*/
#include <iostream>
int boyremoore(int a[], int n)
{
int max_index = 0, count = 1, max_value = 0;
for(int i = 1; i < n; i ++) {
if(a[max_index] == a[i]) {
count ++;
}
else {
count --;
}
if(count == 0) {
max_index = i;
count = 1;
}
}
max_value = a[max_index];
count = 0;
for(int i = 0; i < n; i ++) {
if(a[i] == max_value) {
count ++;
}
}
if(count > n / 2) {
return max_value;
}
else {
return -1;
}
}
int main()
{
int n;
std::cin >> n;
int a[n];
for(int i = 0; i < n; i ++) {
std::cin >> a[i];
}
int d = boyremoore(a, n);
if(d == -1) {
std::cout << "NO";
}
else {
std::cout << "YES " << d;
}
return 0;
}