Cod sursa(job #2187014)

Utilizator OldpugAlex Ionescu Oldpug Data 26 martie 2018 09:44:26
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda bpc10 Marime 0.56 kb
#include <fstream>

std::ifstream in{"elmaj.in"};
std::ofstream out{"elmaj.out"};

int32_t v[1000001];

int main() {
  int32_t n{};
  in >> n;

  for (auto i{1}; i <= n; ++i)
    in >> v[i];

  int32_t cand{-1}, k{};
  for (auto i{1}; i <= n; ++i)
    if (!k) {
      cand = v[i];
      k = 1;
    } else if (cand == v[i])
      k++;
    else
      --k;

  if (cand == -1)
    out << -1 << '\n';
  else {
    int32_t nr{};
    for (auto i{1}; i <= n; i++)
      if (v[i] == cand)
        nr++;

    out << cand << ' ' << nr << '\n';
  }
}