Pagini recente » Rating Susan Radu (susanuradu1999) | Istoria paginii runda/rar91/clasament | Cod sursa (job #2450631) | Cod sursa (job #899538) | Cod sursa (job #1464527)
#include <fstream>
using namespace std;
///// DESCRIPTION
// THIS PROGRAM FINDS THE MAJORITY
// ELEMENT FROM A VECTOR OF N ELEMENTS
// MAJ EL = APPEARS AT LEAST (N/2) + 1 TIMES
/////
int freq[2000000001];
int main(int argc, char **argv)
{
// INPUT
int n;
ifstream indata("elmaj.in");
indata >> n;
int max = 0;
for (int i = 1; i <= 2000000000; i++) {
freq[i] = 0;
}
int aux;
for (int i = 0; i < n; i++) {
indata >> aux;
freq[aux]++;
if (freq[aux] > freq[max]) {
max = aux;
}
}
indata.close();
// FIND A MAJORITY ELEMENT AND OUTPUT IT
ofstream outdata("elmaj.out");
if (freq[max] >= (n /2) + 1) {
outdata << max << " " << freq[max];
} else {
outdata << -1 << " " << 0;
}
outdata.close();
return 0;
}