Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boyer Moore’s Voting Algorithm in C++ #1442

Closed
wants to merge 17 commits into from
70 changes: 70 additions & 0 deletions C-Plus-Plus/Algorithms/Boyer_Moore_Majority_Vote.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// C++ Program for finding out
// Majority Element in an array
#include <bits/stdc++.h>
using namespace std;

/* Function to find the candidate with the Majority */
int findCandidate(int a[], int size)
{
int majority_element = 0, count = 1;
for (int i = 1; i < size; i++) {
if (a[majority_element] == a[i])
count++;
else
count--;
if (count == 0) {
majority_element = i;
count = 1;
}
}
return a[majority_element];
}

/* Function to check if the candidate
occurs more than n/2 times */
bool isMajority(int a[], int size, int cand)
{
int count = 0;
for (int i = 0; i < size; i++)

if (a[i] == cand)
count++;

if (count > size / 2)
return 1;

else
return 0;
}

/* Function to print the Majority Element */
void printMajority(int a[], int size)
{
/* Find the candidate for Majority*/
int cand = findCandidate(a, size);

/* Print the candidate if it is Majority*/
if (isMajority(a, size, cand))
cout << " " << cand << " ";

else
cout << "No Majority Element";
}

/* Driver code */
/* int main()
{
int a[] = { 1, 3, 3, 1, 2 };
int size = (sizeof(a)) / sizeof(a[0]);

// Function calling
printMajority(a, size);

return 0;
}

Output - No Majority Element */

/* Time Complexity: O(n). The time complexity is linear.
Auxiliary Space: O(1). No extra space is required */