Cod sursa(job #2042770)
| Utilizator | Data | 19 octombrie 2017 05:09:31 | |
|---|---|---|---|
| Problema | Elementul majoritar | Scor | 90 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.39 kb |
#include <fstream>
#include <map>
std::ifstream cin("elmaj.in");
std::ofstream cout("elmaj.out");
int n, maj = -1, x;
std::map<int, int> counter;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x;
counter[x]++;
if (counter[x] >= n / 2 + 1) {
maj = x;
}
}
if (maj == -1) {
cout << -1;
} else {
cout << maj << " " << counter[maj];
}
return 0;
}
