-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path28.cpp
More file actions
46 lines (43 loc) · 669 Bytes
/
28.cpp
File metadata and controls
46 lines (43 loc) · 669 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
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <vector>
using namespace std;
int MoreThanHalfNum_Solution(vector<int> numbers)
{
int len = numbers.size();
int count = 0, ans;
for (int i = 0; i < len; i++)
{
if (!count)
{
ans = numbers[i];
count = 1;
}
else
{
if (ans == numbers[i])
count++;
else
count--;
}
}
count = 0;
for (int i = 0; i < len; i++)
if (numbers[i] == ans)count++;
if (count * 2 > len)
return ans;
return 0;
}
int main()
{
ios::sync_with_stdio(false);
int n, temp;
vector<int> v;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> temp;
v.push_back(temp);
}
cout << MoreThanHalfNum_Solution(v) << endl;
return 0;
}