Cod sursa(job #1326684)
Utilizator | Ungureanu Daniel danielungureanu | Data | 25 ianuarie 2015 20:29:34 |
---|---|---|---|
Problema | Elementul majoritar | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.6 kb |
#include <fstream>
using namespace std;
ifstream f("elmaj.in");
ofstream g("elmaj.out");
int main()
{
int n, i, cand, k=0;
f>>n;
int v[n];
for(i=0; i<=n; i++)
{
f>>v[i];
}
for(int i=0; i<n; i++)
if(k==0)
{
cand=v[i];
k=1;
}
else
if(v[i]==cand)
k++;
else
k--;
int nr=0;
for(int i=0; i<n; i++)
{
if(v[i]==cand)
nr++;
}
if(nr>n/2)
g<<cand<<" "<<nr;
else
g<<"-1";
return 0;
}