Cod sursa(job #2947645)
| Utilizator | Data | 26 noiembrie 2022 15:26:37 | |
|---|---|---|---|
| Problema | Elementul majoritar | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.7 kb |
#include <fstream>
#define limit 1000005
using namespace std;
ifstream in("elmaj.in");
ofstream out("elmaj.out");
int v[limit];
int n, elmaj, counter, ans;
int main()
{
in>>n;
in>>v[1];
elmaj = v[1];
counter++;
for(int i=2; i<=n; i++)
{
in>>v[i];
if(v[i] == elmaj)
counter++;
else
{
if(counter == 0)
{
elmaj = v[i];
counter++;
}
else
counter--;
}
}
for(int i=1; i<=n; i++)
{
if(v[i] == elmaj)
{
ans++;
}
}
out<<elmaj<<" "<<ans;
return 0;
}
