Pagini recente » Cod sursa (job #427878) | Cod sursa (job #1915565) | Cod sursa (job #837880) | Cod sursa (job #1990112) | Cod sursa (job #2070238)
#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], currentPretendent, noOfAparition);
}
noOfAparition = 0;
for (int i = 1; i<=n; ++i)
noOfAparition += (v[i] == pretendent);
fout << (noOfAparition >= n/2+1 ? currentPretendent : -1);
return 0;
}