Cod sursa(job #2340262)

Utilizator andu2006Alexandru Gheorghies andu2006 Data 10 februarie 2019 10:15:13
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.59 kb
#include<bits/stdc++.h>

using namespace std;
ifstream fin("elmaj.in");
ofstream fout("elmaj.out");
int v[1000000];
int main()
{
    int n,i,lpos=0;
    fin>>n;
    for(i=0;i<n;i++)
    {
        fin>>v[i];
    }
    sort(v,v+n);
    for(i=1;i<n;i++)
    {
        if(v[i]!=v[i-1])
        {
            if(i-lpos>n/2)
            {
                fout<<v[i-1]<<" "<<i-lpos;
                return 0;
            }
            lpos=i;
        }
    }
    if(i-lpos>n/2)
    {
        fout<<v[i-1]<<" "<<i-lpos;
        return 0;
    }
    fout<<-1;
    return 0;
}