Cod sursa(job #2347323)
Utilizator | Data | 18 februarie 2019 17:57:08 | |
---|---|---|---|
Problema | Elementul majoritar | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.63 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("elmaj.in");
ofstream fout("elmaj.out");
long long n, x, nr, castigator, V[1000005], nrap;
int main()
{
fin>>n;
for (int i=1; i<=n; i++)
{
fin>>x;
V[i]=x;
if (nr==0)
{
castigator=x;
nr=1;
} else
{
if (castigator==x)
nr++;
else
nr--;
}
}
for (int i=1; i<=n; i++)
if (V[i]==castigator)
nrap++;
if (nrap>=n/2+1)
fout<<castigator<<" "<<nrap;
else
fout<<-1;
fout<<endl;
return 0;
}