Pagini recente » Cod sursa (job #230392) | Cod sursa (job #1508234) | Cod sursa (job #521445) | Cod sursa (job #46036) | Cod sursa (job #2070241)
#include<fstream>
using namespace std;
int v[1000001];
void addToPretendents (int value, int ¤tPretendent, int & noOfAparition)
{
if (currentPretendent == value)
++noOfAparition;
else if (noOfAparition == 1)
currentPretendent = value;
else --noOfAparition;
}
int main()
{
ifstream fin("elmaj.in");
ofstream fout("elmaj.out");
int n;
int noOfAparition = 0;
int pretendent = 0;
fin >> n;
for (int i = 1; i <= n; ++i)
{
fin >> v[i];
addToPretendents (v[i], pretendent, noOfAparition);
}
noOfAparition = 0;
for (int i = 1; i<=n; ++i)
noOfAparition += (v[i] == pretendent);
fout << (noOfAparition >= n/2+1 ? pretendent : -1);
return 0;
}