Cod sursa(job #2171411)
| Utilizator | Data | 15 martie 2018 12:16:53 | |
|---|---|---|---|
| Problema | Elementul majoritar | Scor | 0 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.57 kb |
#include <fstream>
#include <algorithm>
using namespace std;
ifstream cin("elmaj.in");
ofstream cout("elmaj.out");
void sortMajority(int n, int a[]) {
sort(a, a+n);
int i = 0, ok=0;
while (i < n) {
int j = i;
while (j < n && a[j + 1] == a[i])
j++;
if ((j-i+1) > n/2)
cout << a[i] << ' ' << j, ok=1;
i = j + 1;
}
if(ok==0)
cout << -1;
}
int main()
{
int n, a[100005], i;
cin >> n;
for(i=0; i<n; i++)
cin >> a[i];
sortMajority(n, a);
}
