-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoores.cpp
More file actions
37 lines (34 loc) · 887 Bytes
/
Copy pathmoores.cpp
File metadata and controls
37 lines (34 loc) · 887 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <vector>
using namespace std;
int main() {
int arr[6]= {1 ,1 ,2 ,2 ,1 ,1 };
int n = sizeof(arr)/sizeof(arr[0]);
vector<int> ans;
int maxCount = 1;
for(int i = 1; i < n; i++) {
if(arr[i] == arr[i-1]) {
ans.push_back(arr[i]);
maxCount++;
} else if(arr[i] != arr[i-1] ){
maxCount--;
}
if(maxCount == 0) {
ans.clear();
ans.push_back(arr[i]);
maxCount = 1;
}
}
if(maxCount > 0) {
cout << "Majority element is: " << ans[0] << endl;
int count = 0;
for(int i = 0; i < n; i++) {
if(arr[i] == ans[0]) {
count++;
}
}
cout << "Count is: " << count << endl;
} else {
cout << "No majority element found" << endl;
}
}