Cod sursa(job #2286170)

Utilizator andreiomd1Onut Andrei andreiomd1 Data 19 noiembrie 2018 21:27:59
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb

#include <bits/stdc++.h>

using namespace std;

ifstream f("elmaj.in");
ofstream g("elmaj.out");

const int NMAX=1e6+5;

int N, a[NMAX];

int i;

int main()
{
    f>>N;

    for(i=1; i<=N; i++)
        f>>a[i];

    sort(a+1, a+N+1);

    int val=-1, afis=0;

    for(i=1; i<N; i++)
        if(a[i]==a[i+1])
        {
            int aux=1;

            while(a[i]==a[i+1])
            {
                i++;
                aux++;
            }

            i--;

            if(aux>=(N/2+1))
                val=a[i], afis=aux;
        }

    if(val==-1)
        g<<-1;
    else
        g<<val<<' '<<afis<<'\n';

    return 0;
}