Cod sursa(job #2911743)

Utilizator andrei_C1Andrei Chertes andrei_C1 Data 1 iulie 2022 18:37:37
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.45 kb
#include <bits/stdc++.h>

using namespace std;

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

int main() {
	int n;
	fin >> n;
	vector<int> v(n);

	for(auto &it: v) {
		fin >> it;
	}

	nth_element(v.begin(), v.begin() + v.size() / 2, v.end());
	int med = v[v.size() / 2], cnt = 0;

	for(const auto &it: v) {
		if(it == med) {
			cnt++;
		}
	}

	if(cnt > v.size() / 2) {
		fout << med << " " << cnt << '\n';
	} else {
		fout << "-1\n";
	}
	return 0;
}