Cod sursa(job #3148711)

Utilizator stefanchpStefan Chiper stefanchp Data 3 septembrie 2023 16:42:27
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.55 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;

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

int n;
vector <int> a;

int main()
{
	int x, cand, vot;
	fin >> n;
	for (int i = 1; i <= n; i++)
		fin >> x, a.push_back(x);

	cand = a[0]; vot = 1;
	for (int i = 1; i < n; i++)
		if (cand == a[i])
			vot++;
		else if (vot == 0)
			cand = a[i];
		else
			vot--;
	vot = 0;
	for (int i = 0; i < n; i++)
		if (cand == a[i])
			vot++;
	fout << cand << ' ' << vot;
	return 0;
}