From c57ed6b0f349600948aa7b0e37e277ebdd69554d Mon Sep 17 00:00:00 2001 From: Aritra Bose Date: Sun, 3 Jul 2022 00:17:26 +0530 Subject: [PATCH 1/2] Create Boyer-Moore Majority Voting Algorithm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Boyer-Moore voting algorithm is one of the popular optimal algorithms which is used to find the majority element among the given elements that have more than N/ 2 occurrences. This works perfectly fine for finding the majority element which takes 2 traversals over the given elements, which works in O(N) time complexity and O(1) space complexity. Let us see the algorithm and intuition behind its working, by taking an example – Input :{1,1,1,1,2,3,5} Output : 1 Explanation : 1 occurs more than 3 times. Input : {1,2,3} Output : -1 This algorithm works on the fact that if an element occurs more than N/2 times, it means that the remaining elements other than this would definitely be less than N/2. So let us check the proceeding of the algorithm. First, choose a candidate from the given set of elements if it is the same as the candidate element, increase the votes. Otherwise, decrease the votes if votes become 0, and select another new element as the new candidate. Intuition Behind Working : When the elements are the same as the candidate element, votes are incremented when some other element is found not equal to the candidate element. We decreased the count. This actually means that we are decreasing the priority of winning ability of the selected candidate, since we know that if the candidate is a majority it occurs more than N/2 times and the remaining elements are less than N/2. We keep decreasing the votes since we found some different element than the candidate element. When votes become 0, this actually means that there are the same number of different elements, which should not be the case for the element to be the majority element. So the candidate element cannot be the majority, so we choose the present element as the candidate and continue the same till all the elements get finished. The final candidate would be our majority element. We check using the 2nd traversal to see whether its count is greater than N/2. If it is true, we consider it as the majority element. Steps to implement the algorithm : Step 1 – Find a candidate with the majority – Initialize a variable say i ,votes = 0, candidate =-1 Traverse through the array using for loop If votes = 0, choose the candidate = arr[i] , make votes=1. else if the current element is the same as the candidate increment votes else decrement votes. Step 2 – Check if the candidate has more than N/2 votes – Initialize a variable count =0 and increment count if it is the same as the candidate. If the count is >N/2, return the candidate. else return -1. --- .../Search/majority-element.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Competitive Coding/Search/majority-element.cpp diff --git a/Competitive Coding/Search/majority-element.cpp b/Competitive Coding/Search/majority-element.cpp new file mode 100644 index 000000000..8976218ef --- /dev/null +++ b/Competitive Coding/Search/majority-element.cpp @@ -0,0 +1,40 @@ +// C++ implementation of Boyer-Moore Majority Voting Algorithm +#include +using namespace std; +// Function to find majority element +int findMajority(int arr[], int n) +{ + int i, candidate = -1, votes = 0; + // Finding majority candidate + for (i = 0; i < n; i++) { + if (votes == 0) { + candidate = arr[i]; + votes = 1; + } + else { + if (arr[i] == candidate) + votes++; + else + votes--; + } + } + int count = 0; + // Checking if majority candidate occurs more than n/2 + // times + for (i = 0; i < n; i++) { + if (arr[i] == candidate) + count++; + } + + if (count > n / 2) + return candidate; + return -1; +} +int main() +{ + int arr[] = { 1, 1, 1, 1, 2, 3, 4 }; + int n = sizeof(arr) / sizeof(arr[0]); + int majority = findMajority(arr, n); + cout << " The majority element is : " << majority; + return 0; +} From a18cad1f5bd6a10796cc7879861f1c7d01f6e3f7 Mon Sep 17 00:00:00 2001 From: Aritra Bose Date: Sun, 3 Jul 2022 00:36:38 +0530 Subject: [PATCH 2/2] Update majority-element.cpp --- Competitive Coding/Search/majority-element.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Competitive Coding/Search/majority-element.cpp b/Competitive Coding/Search/majority-element.cpp index 8976218ef..dbda0c6aa 100644 --- a/Competitive Coding/Search/majority-element.cpp +++ b/Competitive Coding/Search/majority-element.cpp @@ -1,6 +1,7 @@ // C++ implementation of Boyer-Moore Majority Voting Algorithm #include using namespace std; + // Function to find majority element int findMajority(int arr[], int n) { @@ -21,15 +22,15 @@ int findMajority(int arr[], int n) int count = 0; // Checking if majority candidate occurs more than n/2 // times - for (i = 0; i < n; i++) { + for (i = 0; i < n; i++) if (arr[i] == candidate) count++; - } if (count > n / 2) return candidate; return -1; } + int main() { int arr[] = { 1, 1, 1, 1, 2, 3, 4 };