Cod sursa(job #3305033)
| Utilizator | Data | 29 iulie 2025 15:42:50 | |
|---|---|---|---|
| Problema | Elementul majoritar | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.8 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("elmaj.in");
ofstream fout("elmaj.out");
int n;
int A[1000001];
int main()
{
fin>>n;
for(int i=1;i<=n;i++)
fin>>A[i];
int cand=-1,nrVoturi=0;
for(int i=1;i<=n;i++)
{
if(nrVoturi==0)
{
cand=A[i];
nrVoturi=1;
}
else
{
if(A[i]==cand)
nrVoturi++;
else
nrVoturi--;
}
}
if(nrVoturi==0)
fout<<-1;
else
{
int rez=0;
for(int i=1;i<=n;i++)
{
if(A[i]==cand)
rez++;
}
if(rez>=n/2+1)
fout<<cand<<" "<<rez;
else
fout<<-1;
}
return 0;
}
