Cod sursa(job #2214044)

Utilizator pacheprotopopescuPake Protopopescu pacheprotopopescu Data 18 iunie 2018 12:28:22
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.55 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("elmaj.in");
ofstream g("elmaj.out");
int i,v[1000001],n,ap,mx,x,elmax;
int main()
{
   f>>n;
   for(i=1;i<=n;i++)
    f>>v[i];
   sort(v+1,v+n+1);
   x=v[1];ap=1;mx=1;
   for(i=2;i<=n;i++)
   {
       if(x==v[i])
       {
           ap++;
           if(ap>mx)
           {
               mx=ap;
               elmax=x;
           }
       }
       else {
               x=v[i];
               ap=1;
            }
   }
   g<<elmax<<" "<<mx;
}