Cod sursa(job #2368119)

Utilizator salagean_brianaSalagean Briana salagean_briana Data 5 martie 2019 13:54:40
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.54 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fi ("elmaj.in");
ofstream fo ("elmaj.out");
int n,i,j,k,cnd,v[1000010],ok,nr;
int main()
{
    fi>>n;
    for(i=1;i<=n;i++)
        fi>>v[i];
    cnd=v[1];
    k=1; i=2;
    while(i<=n)
    {
        if(v[i]==cnd)
            k++;
        else
            k--;
        if(k==-1)
            cnd=v[i],k=1;
        i++;
    }
    for(i=1;i<=n;i++)
        if(v[i]==cnd)
            nr++;
    fo<<cnd<<" "<<nr;
    fi.close();
    fo.close();
    return 0;
}