Pagini recente » Cod sursa (job #2881690) | Cod sursa (job #2941170) | Cod sursa (job #948059) | Solutii Autumn Warmup, Runda 1 | Cod sursa (job #1040011)
#include <fstream>
using namespace std;
int a[1000003], n;
void Citire()
{
int i;
ifstream fin("elmaj.in");
fin >> n;
for (i = 1; i <= n; i++)
fin >> a[i];
fin.close();
}
void ElemMajoritar()
{
int i, contor, x;
contor = 1;
x = a[1];
for (i = 2; i <= n; i++)
if (a[i] == x) contor++;
else
{
contor--;
if (contor < 0)
{
contor = 1;
x = a[i];
}
}
contor = 0;
for (i = 1; i <= n; i++)
if (a[i] == x) contor++;
ofstream fout ("elmaj.out");
if (contor > n / 2)
fout << x << " " << contor << "\n";
else
fout << "-1\n";
fout.close();
}
int main()
{
Citire();
ElemMajoritar();
return 0;
}