Cod sursa(job #1996701)

Utilizator JustGingaGinga Tudor-Adrian JustGinga Data 2 iulie 2017 13:57:02
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.67 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream in ("elmaj.in");
ofstream out ("elmaj.out");
int n, e, l, eMax, lMax, v[1000001];
int main() {
    in >> n;
    for (int i = 1; i <= n; i++) in >> v[i];
    sort (v + 1, v + n + 1);
    e = v[1]; l = 1;
    for (int i = 2; i <= n; i++)
        if (v[i] == e) l++;
        else {
            if (l > lMax) {
                eMax = e;
                lMax = l;
            }
            e = v[i]; l = 1;
        }
    if (l > lMax) {
        eMax = e;
        lMax = 1;
    }
    if (lMax >= n/2 + 1) out << eMax << " " << lMax << '\n';
    else out << -1 << '\n';
    out.close(); return 0;
}