Cod sursa(job #1908674)

Utilizator bogdanmarin69Bogdan Marin bogdanmarin69 Data 7 martie 2017 09:56:49
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include<fstream>
#include<cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
ifstream cin("elmaj.in");
ofstream cout("elmaj.out");
const int MAX = 1e6 + 1, NR = 5;
int v[MAX], n;
int main()
{
    srand(time(NULL));
    cin >> n;
    for(int i = 1; i <= n; ++i)
        cin >> v[i];
    int ok = 0, ans, nrap;
    for(int i = 1; i <= NR && ok == 0; ++i) {
        int poz = rand() % n + 1;
        int ap = 0;
        for(int j = 1; j <= n; ++j)
            if(v[j] == v[poz])
                ++ap;
        if(ap > n / 2) {
            ok = 1;
            ans = v[poz];
            nrap = ap;
        }
    }
    if(ok == 1)
        cout << ans << ' ' << nrap;
    else
        cout << -1;
    return 0;
}