Pagini recente » Monitorul de evaluare | Cod sursa (job #443390) | Cod sursa (job #118918) | Istoria paginii utilizator/periosoare | Cod sursa (job #2076500)
//package com.company;
import java.io.*;
import java.util.HashMap;
import java.util.StringTokenizer;
public class Main {
private static PrintWriter printWriter;
private static MyScanner scanner;
public static void main(String[] args) throws IOException {
scanner = new MyScanner("elmaj.in");
printWriter = new PrintWriter("elmaj.out");
HashMap<Integer, Integer> hashTable = new HashMap<>();
int x, n;
boolean found = false;
int foundKey = 0;
int foundValue = 0;
n = scanner.nextInt();
for (int i = 0; i < n; i++) {
x = scanner.nextInt();
int newValue = 1;
if (hashTable.containsKey(x)) {
newValue = hashTable.get(x) + 1;
}
if (newValue > n/2) {
found = true;
foundKey = x;
foundValue = newValue;
}
hashTable.put(x, newValue);
}
if (found) {
printWriter.printf("%d %d", foundKey, foundValue);
} else {
printWriter.print(-1);
}
printWriter.close();
scanner.close();
}
private static class MyScanner
{
private BufferedReader bufferedReader;
private StringTokenizer stringTokenizer;
MyScanner(String filename) throws FileNotFoundException {
bufferedReader = new BufferedReader(new FileReader(filename));
}
private String next() throws IOException {
while(stringTokenizer == null || !stringTokenizer.hasMoreElements()) {
stringTokenizer = new StringTokenizer(bufferedReader.readLine());
}
return stringTokenizer.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt( next() );
}
void close() throws IOException {
bufferedReader.close();
}
}
}