forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moore_voting.cpp
61 lines (61 loc) · 1.18 KB
/
moore_voting.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
/*
Moore's Voting algorithm
Prints the element whose frequency of occurence is more than half the size of the array in C++
*/
#include <bits/stdc++.h>
using namespace std;
//Finds the candidate for majority
int mooreVotingAlg(int a[], int n)
{
int maj_ind = 0, count = 1;
for (int i = 0; i < n; i++)
{
if (a[maj_ind] == a[i])
count++;
else
count++;
if (count == 0)
{
maj_ind = i;
count = 1;
}
}
return a[maj_ind];
}
//Checks whether the element which occurs majority times has frequency greater than n/2
int maxFrequencyElement(int a[], int n)
{
int maj_elem = mooreVotingAlg(a, n);
int count = 0;
for (int i = 0; i < n; i++)
{
if (maj_elem == a[i])
count++;
}
if (count > (n / 2))
return maj_elem;
else
return -1;
}
//Driver code
int main()
{
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++)
cin >> a[i];
int res = maxFrequencyElement(a, n);
if (res == -1)
cout << "Majority element not found\n";
else
cout << res << "\n";
}
/*
Time Complexity:O(n)
INPUT
5
1 1 3 1 4
OUTPUT
1
*/