Cod sursa(job #2084819)

Utilizator sandu.m.mdMorari Sandu sandu.m.md Data 9 decembrie 2017 12:08:32
Problema Elementul majoritar Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.57 kb
#include <iostream>
#include <map>
#include <fstream>

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


int main(){
map<int, int> vec;
int n, element, max = 0, key;
    fin >> n;
    for(int i = 0; i < n; i++){
        fin >> element;
        vec[element]++;
    }

    for(map<int, int>::iterator it = vec.begin(); it != vec.end(); it++){
        if(max < it->second){
            max = it->second;
            key = it->first;
        }
    }

    if(max >= n / 2 + 1)
        fout << key << " " << vec[key];
    else 
        fout << "-1";
    return 0;
}