-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day-3-majority-element-i.cpp
45 lines (42 loc) · 1.17 KB
/
Day-3-majority-element-i.cpp
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
38
39
40
41
42
43
44
45
// #include <bits/stdc++.h>
// int findMajorityElement(int arr[], int n) {
// // Moore's voting algorithm
// int majorityElement = arr[0];
// int cnt = 1;
// for(int i=1; i<n; i++){
// if(arr[i] == majorityElement){
// cnt++;
// }else{
// cnt--;
// if(cnt==0){
// majorityElement = arr[i];
// cnt = 1;
// }
// }
// }
// // first step either gives the majority element or gives some element that may
// // be eligible for majority element (edge case when cnt of an element is equal
// // to n/2 then it is not majority element but majorityElement contains it)
// cnt = 0;
// for(int i=0; i<n; i++) if(arr[i] == majorityElement) cnt++;
// return (cnt > n/2) ? majorityElement : -1;
// }
class Solution {
public:
int majorityElement(vector<int>& nums) {
int n = nums.size();
// approach: forming pairs of (a, b) where a!=b
int cnt = 0, element = INT_MIN;
for(int i=0; i<n; i++){
if(nums[i]==element){
cnt++;
}else if(cnt==0){
cnt = 1;
element = nums[i];
}else{
cnt--;
}
}
return element;
}
};