Cod sursa(job #3186478)

Utilizator alex_0747Gheorghica Alexandru alex_0747 Data 23 decembrie 2023 11:51:45
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.45 kb
#include <iostream>
#include <fstream>
#include <unordered_map>
using namespace std;

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

unordered_map <int, int> M;

int main()
{
    int i, x, n, el = 0;
    fin >> n;
    for (i = 1; i <= n; i++)
    {
        fin >> x;
        M[x]++;
        if (M[x] > n / 2)
            el = x;
    }
    if (el == 0) fout << "-1\n";
    else fout << el << " " << M[el] << "\n";
    return 0;
}