From 2259537cd9ffd1d0a3e7bac6e213b5eca59058d2 Mon Sep 17 00:00:00 2001 From: Abhishek Padhy Date: Tue, 27 Apr 2021 22:30:10 +0530 Subject: [PATCH] chore(CPlusPlus): add moore voting algo (#268) --- algorithms/CPlusPlus/Arrays/boyer_more.cpp | 66 ++++++++++++++++++++++ algorithms/CPlusPlus/README.md | 1 + 2 files changed, 67 insertions(+) create mode 100644 algorithms/CPlusPlus/Arrays/boyer_more.cpp diff --git a/algorithms/CPlusPlus/Arrays/boyer_more.cpp b/algorithms/CPlusPlus/Arrays/boyer_more.cpp new file mode 100644 index 00000000..6f21d447 --- /dev/null +++ b/algorithms/CPlusPlus/Arrays/boyer_more.cpp @@ -0,0 +1,66 @@ +//majority element of an array +// element with frequency > (n/2) {n: size of array} +//time complexity : O(n) || space complexity : O(1) + +#include + +using namespace std; + +void ismaj(int arr[],int n,int exp_maj){ // checks the if the candidate element is actually the majority element or not + + int i=0; + int count =0; + for(i=0;i(n/2)){ + cout<<"majority element : "<>size; + + int *arr = new int[size]; + + for(int i=0;i>arr[i]; + } + + int result = majelement(arr,size); + ismaj(arr,size,result); + return 0; +} diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index e2c73521..a4fd7175 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -6,6 +6,7 @@ 3. [Left Rotation](Arrays/left-rotation.cpp) 4. [Max Subarray Sum](Arrays/max-subarray-sum.cpp) 5. [Shift Negatives](Arrays/shift-negatives.cpp) +6. [Boyer–Moore Voting Algorithm](Arrays/boyer_more.cpp) ## Graphs 1. [Bellman Ford Algorithm](Graphs/bellmam-ford.cpp)