Cod sursa(job #1455501)

Utilizator Player1Player 1 Player1 Data 28 iunie 2015 03:48:38
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.47 kb
#include <stdio.h>
#include <map>

using namespace std;

int occurances[1000010];

int main(){
	freopen("elmaj.in","r",stdin);
	freopen("elmaj.out","w",stdout);

	int N, x, i;
	
	bool flag = false;
	int candidate;

	scanf("%d ", &N);

	for (i=0; i<N; i++){
		scanf("%d ", &x);
		occurances[x] ++;
		if(occurances[x] >= N/2 +1){
			candidate = x;				
			flag = true;
		}
	}

	if(flag == false)
		printf("-1");
	else
		printf("%d %d", candidate, occurances[candidate]);
	return 0;
}