-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmajority.cpp
More file actions
35 lines (29 loc) · 793 Bytes
/
Copy pathmajority.cpp
File metadata and controls
35 lines (29 loc) · 793 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
#include <iostream>
#include <vector>
using namespace std;
int main() {
//if the array is sorted
int arr[8] = {2,2,2,2,3,3,3,3};
int n = sizeof(arr) / sizeof(arr[0]);
int count = 1;
vector<int> ans;
ans.push_back(arr[0]);
for(int i = 1; i < n; i++) {
if(ans[0] == arr[i]) {
count++;
}
else {
if(count > n/2){
cout<< "Majority element is: " << ans[0] << endl;
cout<< "Count is: " << count << endl;
return 0;
}else {
ans.clear();
ans.push_back(arr[i]);
count = 1;
}
}
}
cout << "No majority element found" << endl;
return -1; // No majority element found
}