diff --git a/Problems and General Algos/Moore's_Voting_Algo.cpp b/Problems and General Algos/Moore's_Voting_Algo.cpp new file mode 100644 index 0000000..13fe7a8 --- /dev/null +++ b/Problems and General Algos/Moore's_Voting_Algo.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +//Majority element is the element which appears more than n/2 times in an array, +//where n is the size of the array. + +//Function to calculate the majority element +int fun(vector v){ + //Variable 'ele' to store the majority element + int ele=v[0]; + int k=1; + for(int i=1;i v({3,2,4,3,3,3}); + int ans=fun(v); + int k=0; + for(int i=0;iv.size()/2) + cout<<"Majority element is "< +using namespace std; + +int fun(vector& height){ + int n=height.size(); + if(n==0) return 0; + stack s; + int top,distance,length; + int curr=0; + int ans=0; + while(currheight[s.top()]){ + top=s.top(); + s.pop(); + if(s.empty()) + break; + distance=curr-s.top()-1; + length=((height[curr] v({0,1,0,2,1,0,1,3,2,1,2,1}); + cout<<"The amount of trapped rainwater is "<