Pagini recente » Cod sursa (job #541842) | Clasament simulare_oji_2023_clasa_9_14_martie | Cod sursa (job #903917) | Cod sursa (job #3147012) | Cod sursa (job #942136)
Cod sursa(job #942136)
#include<fstream>
using namespace std;
ifstream f("elmaj.in");
ofstream g("elmaj.out");
int v[1000000],n;
int Majority_Element()
{
int i=0,candidate=v[0],votes=0,counter=0;
while(i<n)
{
if(v[i]!=candidate)
votes--;
if(votes==0)
{
candidate=v[i];
votes=1;
}
i++;
}
for(i=0;i<n;i++)
if(v[i]==candidate)
counter++;
if(counter<n/2+1)
g<<-1<<"\n";
else
g<<candidate<<" "<<counter<<"\n";
}
void read_number_and_process()
{
f>>n;
int i;
for(i=0;i<n;i++)
f>>v[i];
Majority_Element();
}
int main()
{
read_number_and_process();
}