Cod sursa(job #2978351)

Utilizator eneagoeEugen Neagoe eneagoe Data 13 februarie 2023 18:14:41
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.53 kb
#include <iostream>
#include <fstream>
#include <unordered_map>

using namespace std;

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

unordered_map<int, int> f;

int main(void)
{
    int n, m = -1;
    bool found = false;

    fin >> n;

    for(int i = 0; i < n; i++) {
        int x;

        fin >> x;

        f[x]++;
    }

    for(auto &p: f)
        if(p.second >= (n / 2 + 1))
            m = p.first, found = true;

    if(found)
        fout << m << ' ' << f[m] << endl;
    else
        fout << -1 << endl;

    return 0;
}