Cod sursa(job #2394884)

Utilizator S_AndyAndrei S S_Andy Data 2 aprilie 2019 04:26:27
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.52 kb
#include <fstream>
#include <algorithm>

using namespace std;

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

int main()
{
	int n, *v, elmaj, s = 0;
	fin >> n;
	v = new int[n];
	for (int i = 0; i < n; ++i) {
		fin >> v[i];
		if (s == 0) {
			elmaj = v[i];
			s = 1;
		}
		else if (elmaj == v[i]) {
			++s;
		}
		else {
			--s;
		}
	}
	s = 0;
	for (int i = 0; i < n; ++i) {
		if (v[i] == elmaj) {
			++s;
		}
	}
	if (s > n / 2) {
		fout << elmaj << " " << s;
	}
	else {
		fout << -1;
	}
}