Mai intai trebuie sa te autentifici.
Cod sursa(job #3349813)
| Utilizator | Data | 2 aprilie 2026 16:43:57 | |
|---|---|---|---|
| Problema | Elementul majoritar | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.82 kb |
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main() {
ifstream fin("elmaj.in");
ofstream fout("elmaj.out");
int n;
fin >> n;
vector<int> lista(n);
for (int i = 0; i < n; i++) {
fin >> lista[i];
}
bool gasit = false;
int incercari = 20;
for (int i = 0; i < incercari; i++) {
int index = rand() % n;
int candidat = lista[index];
int aparitii = 0;
for (int j = 0; j < n; j++) {
if (lista[j] == candidat) {
aparitii++;
}
}
if (aparitii >= n / 2 + 1) {
fout << candidat << " " << aparitii;
gasit = true;
break;
}
}
if (!gasit) {
fout << "-1";
}
fin.close();
fout.close();
return 0;
}