Cod sursa(job #2955728)

Utilizator VasAlexVasiluta Alex VasAlex Data 17 decembrie 2022 17:52:06
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda cnsh_16_12 Marime 0.69 kb
#include <bits/stdc++.h>
using namespace std;
#ifndef offline
string nume_problema = "elmaj";
ifstream fin(nume_problema + ".in");
ofstream fout(nume_problema + ".out");
#define cin fin
#define cout fout
#endif
int v[1000002], n, xx, k;

int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> v[i];
        if (k == 0)
        {
            xx = v[i];
            k++;
        }
        else
        {
            if (v[i] == xx)
                k++;
            else
                k--;
        }
    }
    k = 0;
    for (int i = 1; i <= n; i++)
        if (v[i] == xx)
            k++;
    if (k > n / 2)
        cout << xx << " " << k;
    else
        cout << -1;
    return 0;
}