Cod sursa(job #2489750)

Utilizator iProgramInCppiProgramInCpp iProgramInCpp Data 9 noiembrie 2019 11:39:55
Problema Elementul majoritar Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include<bits/stdc++.h>
using namespace std;
int main()
{
    ifstream ifs("elmaj.in");
    ofstream ofs("elmaj.out");

    int n;
    ifs >> n;
    vector<int> v;
    for(int i = 0; i < n; i++)
    {
        int a; ifs >> a; v.push_back(a);
    }
    ifs.close();

    int entries = n / 2 + 1;
    int countentries = 0;
    int value;
    bool foundSmth = false;

    for(int i = 0; i < v.size(); i++)
    {
        value = v[i];
        for(int j = 0; j < v.size(); j++)
        {
            if(value == v[j])
                countentries++;
        }
        if(countentries >= entries) {
            foundSmth = true;
            break;
        }
        countentries = 0;
    }
    if(foundSmth) ofs << value << " " << countentries; else ofs << -1;

    ofs.close();
    return 0;
}