Cod sursa(job #1971445)

Utilizator BeatriceBBeatrice Roxana BeatriceB Data 20 aprilie 2017 13:57:44
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.47 kb
#include <fstream>
using namespace std;
#define in "elmaj.in"
#define out "elmaj.out"
#define N 1000005
ifstream f(in);
ofstream g(out);
int n, frecv[N], sol;

int main ()
{
    f>>n;
    for(int i=0; i<n; i++)
    {
        int x;
        f>>x;
        frecv[x]++;
        if(frecv[x]>frecv[sol])
            sol=x;
    }
    if(frecv[sol]>=n/2+1)
        g<<sol<<" "<<frecv[sol];
    else
        g<<-1;
    f.close();
    g.close();
    return 0;
}