-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYaroslavAndPermutaions.java
More file actions
42 lines (42 loc) · 1.21 KB
/
YaroslavAndPermutaions.java
File metadata and controls
42 lines (42 loc) · 1.21 KB
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
import java.util.*;
public class YaroslavAndPermutaions {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), index = 0;
int[] a = new int[n];
while(sc.hasNextInt()) {
a[index] = sc.nextInt();
index++;
}
sc.close();
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < n; i++) {
if(map.containsKey(a[i])) {
int old = map.get(a[i]);
map.put(a[i], old + 1);
} else {
map.put(a[i], 1);
}
}
Set<Integer> keys = map.keySet();
Iterator<Integer> it = keys.iterator();
int max = 0;
while(it.hasNext()) {
int value = map.get(it.next());
max = value > max ? value : max;
}
if(n % 2 == 0) {
if(max >= (n / 2) + 1) {
System.out.print("NO");
} else {
System.out.print("YES");
}
} else {
if(max >= (n + 1) / 2 + 1) {
System.out.print("NO");
} else {
System.out.print("YES");
}
}
}
}