Pagini recente » Cod sursa (job #2672001) | Cod sursa (job #1218157) | Cod sursa (job #1046552) | Cod sursa (job #727857) | Cod sursa (job #942137)
Cod sursa(job #942137)
#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(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();
}