Algorithm


Problem Name: 2 AD-HOC - beecrowd | 2469

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2469

Grades

 

By OBI - Olimpíada Brasileira de Informática 2014 BR Brazil

Timelimit: 1

Professor Archimedes needs your help to find out which is the most common notes between the notes that students took in his last race. The class has N students and your program must print the note that appears more often in N notes list. If more than more frequent note, you must print the biggest one! For example, if the class has N = 10 students and grades are [20, 25, 85, 40, 25, 90, 25, 40, 55, 40], the most common grades are 25 and 40, taking place three times each . Your program should then print 40.

 

Input

 

The input consists of two lines. The first line contains an integer N, the number of students in the class. The second line contains N integers, which is the list of students' grades.

 

Output

 

Your program must print a single line containing just a number, the most frequent note of the list.

 

 

 

Input Sample Output Sample

10
20 25 85 40 25 90 25 40 55 40

40

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define true 1
#define false 0
#define MAXSIZE 100100

typedef struct Notas{

	unsigned nota;
	unsigned posicao;

} Notas;

int compara(Notas *, Notas *);

int main (void)
{

	unsigned i, n, tmp;
	Notas notas[MAXSIZE];

	scanf("%u", &n);
	memset(notas, 0, sizeof(notas));

	while (n--)
	{

		scanf("%u", &tmp);
		notas[tmp].nota++;
		notas[tmp].posicao = tmp;

	}

	qsort(notas, MAXSIZE, sizeof(Notas), compara);
	printf("%u\n", notas[0].posicao);

}

int compara(Notas *a, Notas *b)
{

	if (a->nota == b->nota)
	{

		if (a->posicao > b->posicao)
			return -1;
		else
			return 1;

	}
	else if (a->nota > b->nota)
		return -1;
	else
		return 1;

}
Copy The Code & Try With Live Editor

Input

x
+
cmd
10
20 25 85 40 25 90 25 40 55 40

Output

x
+
cmd
40

#2 Code Example with C++ Programming

Code - C++ Programming


#include <cstdio>
#include <map>
using namespace std;
int main() {
    int n, i, resposta, vezes = 0, davez;
    map < int, int> mapa;
    map<int, int>::iterator it;
    scanf("%d", &n);
    for (i = 0; i  <  n; i++) {
        scanf("%d", &davez);
        if (mapa.find(davez) != mapa.end())
            mapa[davez] += 1;
        else
            mapa[davez] = 1;
    }
    for (it = mapa.begin(); it != mapa.end(); it++) {
        if ((*it).second > vezes) {
            resposta = (*it).first;
            vezes = (*it).second;
        } else if ((*it).second == vezes && (*it).first > resposta)
            resposta = (*it).first;
    }
    printf("%d\n", resposta);
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
10
20 25 85 40 25 90 25 40 55 40

Output

x
+
cmd
40
Advertisements

Demonstration


Previous
#2468 Beecrowd Online Judge Solution 2468 Map Solution in C, C++, Java, Js and Python
Next
#2470 Beecrowd Online Judge Solution 2470 Jogo da Memória Solution in C, C++, Java, Js and Python