Cod sursa(job #2666919)

Utilizator PletoPletosu Cosmin-Andrei Pleto Data 2 noiembrie 2020 16:41:16
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.55 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("elmaj.in");
ofstream fout("elmaj.out");

unordered_map<int, int> myMap;

int main() {
    int N;
    fin >> N;
    for (int x, i = 0; i < N; ++i) {
        fin >> x;
        if (myMap.find(x) == myMap.end()) {
            myMap[x] = 1;
        } else {
            myMap[x] += 1;
        }
    }

    for (auto &x: myMap) {
        if (x.second >= (N / 2 + 1)) {
            fout << x.first << ' ' << x.second << '\n';
            return 0;
        }
    }
        
    fout << -1 << '\n';
    return 0;
}