Skip to content

Commit

Permalink
Create MajorityElement
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Margeloiu authored Jul 19, 2016
1 parent dcb7908 commit bc92114
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions MajorityElement
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# include <bits/stdc++.h>
using namespace std;
int main ()
{
/* Given an array of n elements, find the majority element.
A majority element occurs at least n/2 + 1 times.
If it doesn't exist print -1.
*/
int n=11; // number of elements (indexes 1-11)
//index 0 1 2 3 4 5 6 7 8 9 10 11
int A[]={0,1,1,4,5,1,6,6,1,1,3, 1};

sort (A+1, A+n+1);

// A[n/2+1] can be the majority element
int majorityElement=A[n/2+1];
int nrOccurrences=0;

for (int i=1; i<=n; ++i)
if (A[i]==majorityElement) ++nrOccurrences;

if (nrOccurrences >= n/2 + 1) {
cout<<"The majority element is "<<majorityElement;
cout<<" and it appears "<<nrOccurrences<<" times";
} else cout<<"-1\n";

return 0;
}

0 comments on commit bc92114

Please sign in to comment.