Cod sursa(job #3205221)

Utilizator BurloiEmilAndreiBurloi Emil Andrei BurloiEmilAndrei Data 19 februarie 2024 09:23:16
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define pb push_back

const string FILE_NAME = "elmaj";
const int MAX_N = 1e6;

int v[MAX_N + 5];

int main() {
    #ifndef LOCAL
        ifstream cin(FILE_NAME + ".in");
        ofstream cout(FILE_NAME + ".out");
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, i, cand, num;

    cin >> n;
    for (i = 0; i < n; i++) {
        cin >> v[i];
    }

    cand = v[0];
    num = 0;
    for (i = 0; i < n; i++) {
        if (v[i] == cand) {
            num++;
        } else {
            num--;
            if (num < 0) {
                cand = v[i];
                num = 0;
            }
        }
    }

    num = 0;
    for (i = 0; i < n; i++) {
        if (v[i] == cand) {
            num++;
        }
    }

    cout << cand << " " << num;
    return 0;
}