Cod sursa(job #2386759)
Utilizator | Vlad Popa vlad_popa | Data | 23 martie 2019 17:09:03 |
---|---|---|---|
Problema | Elementul majoritar | Scor | 100 |
Compilator | cpp-32 | Status | done |
Runda | Arhiva educationala | Marime | 0.49 kb |
#include <iostream>
#include <fstream>
#include <unordered_map>
int main ()
{
std::ifstream in("elmaj.in");
int N;
in >> N;
std::unordered_map<int, int> m;
for (int i = 0; i < N; ++ i) {
int x;
in >> x;
m[x]++;
}
std::ofstream out("elmaj.out");
bool found = false;
for (auto entry : m) {
if (entry.second > N/2) {
out << entry.first << " " << entry.second << "\n";
found = true;
break;
}
}
if (!found) {
out << "-1\n";
}
out.close();
in.close();
return 0;
}