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

Moore's Voting Algorithm #3203

Open
2 of 4 tasks
HellspawnXerxes opened this issue Oct 1, 2022 · 4 comments
Open
2 of 4 tasks

Moore's Voting Algorithm #3203

HellspawnXerxes opened this issue Oct 1, 2022 · 4 comments

Comments

@HellspawnXerxes
Copy link
Contributor

HellspawnXerxes commented Oct 1, 2022

This is a(n):

  • New algorithm
  • Update to an existing algorithm
  • Error
  • Proposal to the Repository

Details:

Moore's Voting Algorithm is to find a majority element in an array. It basically check if the occurrence of an element in the array is greater than the array size then that element is in majority.
The code is as follows: -

#include<bits/stdc++.h>
using namespace std;

int moore_voting(int [], int);

int main()
{
    int n, ar[50];
    cout << "Enter the size of the array: ";
    cin >> n;
    for(int i = 0; i<n; i++)
    {
        cout << "ar[" << i << "] = ";
        cin >> ar[i];
    }
    cout << "The majority element in the array is: ";
    cout << moore_voting(ar, n);
}

int moore_voting(int arr[], int size)
{
    //Phase 1: - Finds a suitable candidate for the majority element
    int res = 0, count = 1;     //
    for(int i = 1; i<size; i++)    //Traversing the array
    {
        if(arr[res] == arr[i])   //Checking if first element is same as ith element
            count++;     //If yes then incrementing the count by 1
        else
            count--;     //Decrementing the count if ith element is not equal to first element
        if(count==0)     //If at any point the count becomes 0 while decrementing
        {
            res = i;     //Updating the ith element as result since it's not a suitable 
            count = 1;   //candidate for majority. Also resetting the count of majority element
        }
    }

    //Phase 2: - Checks if the candidate selected is actually in majority or not
    count = 0;
    for(int i = 0; i<size; i++)
    {
        if(arr[res] == arr[i])
        
            count++;              //Line 36-44: - Checks if the candidate
    }                             //element is a majority element or not
    if(count <= size/2)
        res = -1;
    return res;
}
@Yukino2002
Copy link

Hey, I would like to take this issue up.

@ZoranPandovski
Copy link
Owner

@Yukino2002 sure, just make a PR

@HellspawnXerxes
Copy link
Contributor Author

hey @ZoranPandovski pls have a look at #3217. i think i did it correctly. if there are any amends to be made pls tell me.

@shivesh41kr
Copy link
Contributor

@ZoranPandovski Please have a look on this - #3314 Code with time and space complexity and well explained.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants