Cod sursa(job #3305996)
| Utilizator | Data | 6 august 2025 14:27:46 | |
|---|---|---|---|
| Problema | Elementul majoritar | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.63 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin ("elmaj.in");
ofstream fout ("elmaj.out");
int v[1000001];
int main(){
int n, i;
int nrV, cand;
fin >> n;
nrV=0, cand=-1;
for (i=1; i<=n; i++){
fin >> v[i];
if (nrV==0){
cand=v[i];
nrV=1;
}
else if (cand==v[i]){
nrV++;
}
else {
nrV--;
}
}
if (nrV>0){
int cnt=0;
for (i=1; i<=n; i++){
if (v[i]==cand){
cnt++;
}
}
if (cnt>n/2){
fout << cand << " " << cnt;
}
else {
fout << "-1";
}
}
return 0;
}
