Cod sursa(job #2312584)

Utilizator georgiansofSofronea Georgian georgiansof Data 5 ianuarie 2019 02:06:47
Problema Elementul majoritar Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.34 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin("elmaj.in");
ofstream fout("elmaj.out");

int main()
{
	int n,i,x,cand,ct=1;
	fin>>cand;
	for(i=2;i<=n;i++)
	{
		fin>>x;
		if(cand==x) ct++;
		else if(!ct) cand=x,ct=1;
		     else ct--;
	}
	fin.close();
	if(ct>n/2) fout<<cand<<" "<<ct<<"\n";
	else fout<<-1;
	fout.close();
	return 0;
}