Cod sursa(job #1999187)
Utilizator | Ginga Tudor-Adrian JustGinga | Data | 10 iulie 2017 15:43:38 |
---|---|---|---|
Problema | Elementul majoritar | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.6 kb |
#include <fstream>
using namespace std;
ifstream in ("elmaj.in");
ofstream out ("elmaj.out");
int n, x = -1, k, a[1000001];
int main()
{
in >> n;
for (int i = 1; i <= n; i++) in >> a[i];
for (int i = 1; i <= n; i++)
if (k == 0) { x = a[i]; k++; }
else if (x == a[i]) k++;
else k--;
if (x == -1) out << -1 << '\n';
else
{
k = 0;
for (int i = 1; i <= n; i++)
if (a[i] == x)
k++;
if (k > n/2) out << x << " " << k << '\n';
else out << -1 << '\n';
}
out.close(); return 0;
}