-
Notifications
You must be signed in to change notification settings - Fork 0
/
TheMonkAndPrateek.java
49 lines (47 loc) · 1.28 KB
/
TheMonkAndPrateek.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/** #hash-table #hash-table */
import java.util.HashMap;
import java.util.Scanner;
class TheMonkAndPrateek {
public static int getHashValue(int n) {
int ret = n;
int digitsSum = 0;
while (n > 0) {
digitsSum += n % 10;
n /= 10;
}
return (ret ^ digitsSum);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
HashMap<Integer, Integer> map = new HashMap<>();
int maxValue = 0;
int collisions = 0;
int maxCollisionsMinValue = 0;
int collisionsTimes = 0;
int val, key, times;
for (int i = 0; i < n; i++) {
val = sc.nextInt();
key = getHashValue(val);
if (map.containsKey(key)) {
collisions++;
times = map.get(key) + 1;
map.replace(key, times);
if (times == collisionsTimes) {
maxCollisionsMinValue = Math.min(maxCollisionsMinValue, key);
} else if (times > collisionsTimes) {
maxCollisionsMinValue = key;
collisionsTimes = times;
}
} else {
map.put(key, 0);
maxValue = Math.max(key, maxValue);
}
}
if (collisions == 0) {
System.out.println(maxValue + " " + 0);
} else {
System.out.println(maxCollisionsMinValue + " " + collisions);
}
}
}