

实现代码:
java
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] freq = new int[10]; // 1~9
for (int i = 0; i < n; i++) {
int x = scanner.nextInt();
freq[x]++;
}
int q = n / 9;
int r = n % 9;
int countPlusOne = 0;
boolean possible = true;
for (int i = 1; i <= 9; i++) {
if (freq[i] != q && freq[i] != q + 1) {
possible = false;
break;
}
if (freq[i] == q + 1) {
countPlusOne++;
}
}
if (countPlusOne != r) {
possible = false;
}
System.out.println(possible ? "YES" : "NO");
}