Cod sursa(job #3289099)

Utilizator Caleb_007Sighiartau Achim Caleb Caleb_007 Data 25 martie 2025 18:05:09
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#define _CRT_SECURE_NO_DEPRECATE
#include <fstream>
#include <cstring>
#include <stdlib.h>
#include <cstdio>
#include <cstdlib>
#include <algorithm>

using namespace std;

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


int A[1000000];

void readNum(int &n, FILE *in)
{ 
    n = 0;
    char c = fgetc(in);
    while (c == ' ' || c=='\n') c = fgetc(in);
    while (c!=EOF && c >= '0' && c <= '9') {
        n = n * 10 + c - 48;
        c = fgetc(in);
    }
}

int lmax, emax;

int main()
{
    int n, x;

    FILE* in = fopen("elmaj.in", "r");

    readNum(n, in);

    for (int i = 0; i < n; i++)
        readNum(A[i], in);

    sort(A, A + n);

    int l = 1;
    for (int i = 1; i < n; i++)
        if (A[i] == A[i - 1]) {
            l++;
            if (l > lmax) {
                lmax = l;
                emax = A[i];
            }
        }
        else l = 1;

    fout << emax <<" " << lmax;

    return 0;
}