Cod sursa(job #2971525)

Utilizator Mihai_OctMihai Octavian Mihai_Oct Data 27 ianuarie 2023 15:35:11
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.59 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("elmaj.in");
ofstream fout("elmaj.out");
int n, i, c, ori;
int a[1000002];

int main() {
    ios_base::sync_with_stdio(false);
    fin.tie(NULL);
    fin >> n;
    for(i = 1; i <= n; i++) {
        fin >> a[i];
        if(ori == 0) {
            c = a[i];
            ori = 1;
        }
        else if(c == a[i]) ori++;
        else ori--;
    }
    ori = 0;
    for(i = 1; i <= n; i++) {
        if(c == a[i]) ori++;
    }
    if(ori > n / 2) fout << c << " " << ori;
    else fout << -1;

    return 0;

}