Pagini recente » Cod sursa (job #1635231) | Cod sursa (job #2105037) | Cod sursa (job #922831) | Cod sursa (job #2030931) | Cod sursa (job #2310464)
#include <fstream>
#define NMAX 1e6
std::ifstream fin("elmaj.in");
std::ofstream fout("elmaj.out");
int n, maj = -1, count = 0;
int numbers[NMAX];
int main() {
fin >> n;
for (int i = 0; i < n; i++) {
fin >> numbers[i];
if (count == 0) {
count = 1;
maj = numbers[i];
}
else if (numbers[i] == maj) {
++count;
}
else {
--count;
}
}
if (count == 0) {
cout << -1;
return 0;
}
count = 0;
for (int i = 0; i < n; i++) {
if (numbers[i] == maj) {
++count;
}
}
if (count >= n / 2 + 1) {
fout << maj << " " << count;
}
else {
fout << -1;
}
return 0;
}