chore(CPlusPlus): add moore voting algo (#268)
parent
34d7f59beb
commit
2259537cd9
|
@ -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<iostream>
|
||||
|
||||
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;i++){
|
||||
if(arr[i]==arr[exp_maj]){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
if(count>(n/2)){
|
||||
cout<<"majority element : "<<arr[exp_maj];
|
||||
}
|
||||
else{
|
||||
cout<<"no majority element found\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int majelement(int arr[],int n){ // majority element function to find the candidate majority element
|
||||
|
||||
int ans_index=0; // setting ans_index initially as the first element
|
||||
int count =1; // setting count of first element as 1
|
||||
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i]==arr[ans_index]){
|
||||
count++;
|
||||
}
|
||||
else{
|
||||
count--;
|
||||
}
|
||||
|
||||
if(count ==0){
|
||||
ans_index=i; // updating ans_index as the ith element
|
||||
count =1; // resetting count
|
||||
}
|
||||
}
|
||||
|
||||
return ans_index;// expected majority element
|
||||
}
|
||||
|
||||
|
||||
int main(){ // main function begins here
|
||||
int size;
|
||||
cout<<"enter the size of the array : ";
|
||||
cin>>size;
|
||||
|
||||
int *arr = new int[size];
|
||||
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<"enter "<<i<<" th element of array : ";
|
||||
cin>>arr[i];
|
||||
}
|
||||
|
||||
int result = majelement(arr,size);
|
||||
ismaj(arr,size,result);
|
||||
return 0;
|
||||
}
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue