-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaroslav-and-permutations.java
49 lines (36 loc) · 1.07 KB
/
yaroslav-and-permutations.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
import java.util.*;
import java.io.*;
public class ProgramJava {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int [] arr = new int[n];
for (int i = 0; i<n; i++){
arr[i] = sc.nextInt();
}
HashMap<Integer, Integer> hm = new HashMap<>();
for (int i = 0; i<n; i++){
hm.put(arr[i], hm.getOrDefault(arr[i], 0)+1);
}
int mx = -1;
int mxKey = -1;
for (Map.Entry<Integer, Integer> entry: hm.entrySet()){
if (mx < entry.getValue()){
mx = entry.getValue();
mxKey = entry.getKey();
}
}
int lefts = 0;
for (Map.Entry<Integer, Integer> entry: hm.entrySet()){
if (entry.getKey()!=mxKey){
lefts = lefts+ entry.getValue();
}
}
if (mx - 1 <= lefts){
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
}