Cod sursa(job #2253268)

Utilizator andaraluca2001Anda Epure andaraluca2001 Data 3 octombrie 2018 20:26:32
Problema Elementul majoritar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
/*#include <iostream>
#include <fstream>

using namespace std;
int v[100001],n;

int cautare(int x)
{
    int cnt=0;
    for(int i=1;i<=n;i++) if(v[i]==x) cnt++;
    if(cnt>n/2+1) return cnt;
    else return -1;
}
int main()
{
    ifstream cin("elmaj.in");
    ofstream cout("elmaj.out");

    int max=-1,val;
    cin>>n;

   for(int i=1;i<=n;i++) cin>>v[i];
    for(int i=1;i<=n;i++)
    {
        int ans=cautare(v[i]);
        if(ans!=-1 && ans>max)
        {
            max=ans;
            val=v[i];
        }
    }

    cout<<val<<" "<<max;

    return 0;
}*/

#include <iostream>
#include <fstream>

using namespace std;
int v[10001];
int main()
{
    ifstream cin("elmaj.in");
    ofstream cout("elmaj.out");

    int n,nr;
    cin>>n;

    for(int i=1;i<=n;i++)
    {
        cin>>nr;
        v[nr]++;
    }
    int max=0,val,e=n/2+1;

    for(int i=1;i<=n;i++)
    {
        if(v[i]>=e && v[i]>max )
        {
            max=v[i];
            val=i;
        }
    }

    if(max>0) cout<<val<<" "<<max;
    else cout<<"-1";

    return 0;
}