Cod sursa(job #2543806)
| Utilizator | Data | 11 februarie 2020 15:48:07 | |
|---|---|---|---|
| Problema | Elementul majoritar | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.59 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("elmaj.in");
ofstream fout("elmaj.out");
int n, v[1000001];
int main()
{
int i, ct=0, cand;
fin >> n;
for(i=1; i<=n; i++)
{
fin >> v[i];
if(ct == 0)
cand=v[i], ct=1;
else
{
if(v[i] != cand)
ct--;
else ct++;
}
}
ct=0;
for(i=1; i<=n; i++)
if(v[i] == cand)
ct++;
if(ct >= n/2+1)
fout << cand << " " << ct;
else fout << -1;
return 0;
}
