Cod sursa(job #1994211)
| Utilizator | Data | 24 iunie 2017 13:15:48 | |
|---|---|---|---|
| Problema | Elementul majoritar | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.64 kb |
#include <fstream>
using namespace std;
ifstream cin("elmaj.in");
ofstream cout("elmaj.out");
long long A[1000005];
int main()
{
int n;
cin>>n;
long long cand;
int vot=0;
int ans=0;
for (int i=1; i<=n; i++){
cin>>A[i];
if (vot == 0){
cand =A[i];
}
if (cand == A[i]){
vot--;
}
else{
vot++;
}
}
for (int i=1; i<=n; i++){
if (A[i]==cand){
ans++;
}
}
if (ans>=(n/2)+1){
cout<<cand<<" "<<ans;
}
else{
cout<<-1;
}
return 0;
}
