Cod sursa(job #2679755)
| Utilizator | Data | 1 decembrie 2020 14:04:26 | |
|---|---|---|---|
| Problema | Elementul majoritar | Scor | 30 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.63 kb |
#include <fstream>
#include <vector>
using namespace std;
ifstream in("elmaj.in");
ofstream out("elmaj.out");
int main()
{
int n;
vector<int> nums;
int count = 0;
int elmaj = -1;
in >> n;
int x;
for (int i = 0; i < n; i++)
{
in >> x;
if (x == elmaj)
count++;
else
{
elmaj = x;
count = 1;
}
nums.push_back(x);
}
count = 0;
for (int i = 0; i < n; i++)
{
if (nums[i] == elmaj)
count++;
}
if (count < n / 2 + 1)
out << "-1";
else
out << elmaj << " " << count;
}