Cod sursa(job #3349775)

Utilizator ameliacullRoibu Amelia Maria ameliacull Data 2 aprilie 2026 14:51:46
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 kb
#include <iostream>
#include <fstream>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ifstream fin("elmaj.in");
    ofstream fout("elmaj.out");

    int n = 0;
    if (!(fin >> n)) return 0;

    int candidat = -1;
    int scor = 0;
    int x;

    for (int i = 0; i < n; i++) {
        fin >> x;
        if (scor == 0) {
            candidat = x;
            scor = 1;
        } else if (x == candidat) {
            scor++;
        } else {
            scor--;
        }
    }

    fin.close();
    fin.open("elmaj.in");
    fin >> x;

    int aparitii = 0;
    for (int i = 0; i < n; i++) {
        fin >> x;
        if (x == candidat) {
            aparitii++;
        }
    }

    if (aparitii >= n / 2 + 1) {
        fout << candidat << " " << aparitii << "\n";
    } else {
        fout << -1 << "\n";
    }

    fin.close();
    fout.close();

    return 0;
}