Cod sursa(job #2012059)

Utilizator AndreiMironMiron Andrei AndreiMiron Data 17 august 2017 19:31:00
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.45 kb
#include <fstream>

using namespace std;

ifstream fin("elmaj.in");
ofstream fout("elmaj.out");
int a[1000001];
int main()
{int n,cand=0,k=0,mk=0,c=0;
fin>>n;
    for(int i=0;i<n;i++){
        int x;

        fin>>x;
        a[c++]=x;
        if(x!=cand and k==0){cand=x;
        k=1;
        }
        else if(x!=cand){k--;}
        else {k++;
        }

    }
    for(int i=0;i<n;i++)
        if(a[i]==cand)mk++;
fout<<cand<<" "<<mk;
}