Cod sursa(job #3372308)

Utilizator Albert_PrimeAlbert Olariu Albert_Prime Data 3 noiembrie 2026 08:50:59
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.42 kb
#include <fstream>
#include <map>
using namespace std;
ifstream cin("elmaj.in");
ofstream cout("elmaj.out");
int main()
{
    map<int,int> vf;
    int n,elmaj=0,x;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>x;
        vf[x]++;
        if(vf[x]>vf[elmaj]){
            elmaj=x;
        }
    }
    if(vf[elmaj]<(n/2)+1){
        cout<<-1;
    }
    else{
        cout<<elmaj<<" "<<vf[elmaj];
    }
}