Problem:
 Score : 700 points    Problem Statement There are N cats.
We number them from 1 through N .  Each of the cats wears a hat.
Cat i says: "there are exactly a_i different colors among the N - 1 hats worn by the cats except me."  Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats.      Constraints  2 ≤ N ≤ 10^5  1 ≤ a_i ≤ N-1         Input Input is given from Standard Input in the following format:  N  a_1  a_2  ...  a_N       Output Print Yes if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print No otherwise.        Sample Input 1 3
1 2 2      Sample Output 1 Yes  For example, if cat 1 , 2 and 3 wears red, blue and blue hats, respectively, it is consistent with the remarks of the cats.       Sample Input 2 3
1 1 2      Sample Output 2 No  From the remark of cat 1 , we can see that cat 2 and 3 wear hats of the same color.
Also, from the remark of cat 2 , we can see that cat 1 and 3 wear hats of the same color.
Therefore, cat 1 and 2 wear hats of the same color, which contradicts the remark of cat 3 .       Sample Input 3 5
4 3 4 3 4      Sample Output 3 No       Sample Input 4 3
2 2 2      Sample Output 4 Yes       Sample Input 5 4
2 2 2 2      Sample Output 5 Yes       Sample Input 6 5
3 3 3 3 3      Sample Output 6 No

Solution (Python):
N = int(input())
As = list(map(int, input().split()))

if len(set(As)) >= 3:
    print("No")
    exit()
elif len(set(As)) == 1:
    print("Yes" if 1 <= As[0] <= N // 2 or As[0] == N - 1 else "No")
    exit()
else:
    p, q = sorted(set(As))
    if p + 1 != q:
        print("No")
        exit()
    t = As.count(p)
    u = N - t
    if t + 1 <= q <= t + u // 2:
        print("Yes")
    else:
        print("No")

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 





N = INT()
a = LIST()

if max(a) - min(a) == 0 and (a[0] == N-1 or a[0] <= N//2):
	print("Yes")
elif max(a) - min(a) == 1 and a.count(min(a))+1 <= max(a) <= a.count(min(a)) + a.count(max(a))//2:
	print("Yes")
else:
	print("No")

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop, heapify
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7


N = INT()
a = LIST()


if len(set(a)) == 1:
	if a[0] == N-1 or a[0] <= N//2:
		print("Yes")
	else:
		print("No")

elif len(set(a)) == 2:
	p, q = sorted(list(set(a)))
	p_cnt = a.count(p)
	q_cnt = a.count(q)
	if q == p+1 and  p_cnt + 1 <= q <= p_cnt + q_cnt//2:
		print("Yes")
	else:
		print("No")

else:
	print("No")

Solution (Python):
N = int(input())
a = list(map(int, input().split()))

L = []

def ans(tf):
  if tf:
    print("Yes")
  else:
    print("No")
  exit()

for x in a:
  for i in range(len(L)):
    if L[i][0] == x:
      L[i][1] += 1
      break
  else:
    L.append([x, 1])
    if len(L) >= 3:
      ans(False)

if len(L) == 1:
  x = L[0][0]
  if x == N - 1 or x <= N // 2:
    ans(True)
  else:
    ans(False)
else:
  x, xn = L[0]
  y, yn = L[1]
  if abs(x - y) != 1:
    ans(False)
    exit()
  if x < y:
    x, y = y, x
    xn, yn = yn, xn
  if yn >= x:
    ans(False)
    exit()
  n = x - yn
  if n <= xn // 2:
    ans(True)
  else:
    ans(False)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
cnt = [[a[0], 0]]
for i in a:
    if cnt[0][0] == i:
        cnt[0][1] += 1
    else:
        if len(cnt) == 1:
            cnt.append([i, 1])
            if not abs(cnt[0][0] - cnt[1][0]) == 1:
                print("No")
                exit()
        else:
            if cnt[1][0] == i:
                cnt[1][1] += 1
            else:
                print("No")
                exit()
cnt.sort()
if len(cnt) == 1:
    print("Yes" if cnt[0][0] <= cnt[0][1] // 2 or cnt[0][0] + 1 == n else "No")
else:
    print("Yes" if 1 <= cnt[1][0] - cnt[0][1] <= cnt[1][1] // 2 else "No")

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    _LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()
    DD = ((1,0),(0,1),(-1,0),(0,-1))

    N = NI()
    a = LI()
    max_a = max(a)
    min_a = min(a)

    if max_a > min_a + 1:
        print('No')
        return

    x = a.count(min_a)
    if max_a == min_a:
        if min_a == N-1 or min_a <= N//2:
            print('Yes')
        else:
            print('No')
        return

    if x < max_a and (N-x) >= (max_a - x) * 2:
        print('Yes')
    else:
        print('No')


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
l = list(map(int,input().split()))
s = list(set(l))
if len(s)> 2:
    print("No")
    exit()
if len(s) == 1:
    if s[0] == n-1 or s[0] <= n//2:
        print("Yes")
    else:
        print("No")
    exit()
if abs(s[0]-s[1])>1:
    print("No")
    exit()
x,y = min(s),max(s)
a = l.count(x)
b = n-a
if a+b//2 >= y and a <= x:
    print("Yes")
else:
    print("No")

Solution (Python):
def main():
    n = int(input())
    col = list(map(int, input().split()))
    col_min, col_max = min(col), max(col)
    if col_min == col_max:
        if col_min == 1 or col_min == n-1:
            print("Yes")
        elif 2*col_min <= n:
            print("Yes")
        else:
            print("No")
    elif col_min + 1 == col_max:
        cnt_min, cnt_max = 0, 0
        for v in col:
            if v == col_min:
                cnt_min += 1
            else:
                cnt_max += 1
        if cnt_max == 1:
            print("No")
        else:
            m = cnt_max//2
            if col_min < cnt_min-1 + 1 or cnt_min-1 + m < col_min:
                print("No")
            else:
                p = col_min - (cnt_min-1)
                if p + cnt_min == col_max:
                    print("Yes")
                else:
                    print("No")
    else:
        print("No")

if __name__ == "__main__":
    main()

Solution (Python):





































def main():
    N = int(input())
    A = list(map(int, input().split()))
    max_a, min_a = max(A), min(A)
    if max_a - min_a > 1:
        print('No')
    elif max_a - min_a == 1:
        k = min_a
        n = len([a for a in A if a == k])
        if k >= n and 2 * (k + 1 - n) <= N - n:
            print('Yes')
        else:
            print('No')
    else:  
        k = min_a
        if 2 * k <= N or k + 1 == N:
            print('Yes')
        else:
            print('No')


if __name__ == '__main__':
    main()

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    A = list(map(int, input().split()))

    MAX = max(A)
    MIN = min(A)
    if MAX == MIN:
        print("Yes" if MAX + 1 == n or 2 * MAX <= n else "No")
    elif MAX - MIN == 1:
        cnt = A.count(MIN)
        print("Yes" if cnt < MAX and cnt + 2 * (MAX - cnt) <= n else "No")
    else:
        print("No")


if __name__ == '__main__':
    resolve()

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
A=sorted(A)
if (A[-1]-A[0])>1:
  print('No')
  exit()
elif (A[-1]-A[0])==0:
  if 1<=A[0] and A[0]<=N//2:
    print('Yes')
    exit()
  else:
    if A[0]+1==N:
      print('Yes')
      exit()    
    else:
      print('No')
      exit()
  
import bisect
a=bisect.bisect_right(A,A[0])

b=N-a
if b==1:
  print('No')
  exit()
if (a-1)+1<=A[0] and A[0]<=(a-1)+b//2 and a+1<=A[-1] and A[-1]<=a+b//2:
  print('Yes')
else:
  print('No')

Solution (Python):
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    a = list(map(int, input().split()))
    
    M, m = max(a), min(a)
    c = a.count(m)
    C = a.count(M)
    
    if M == m:
        if n//2 >= m or m == n-1:
            judge = True
        else:
            judge = False
    else:
        if M - m > 1:
            judge = False
        elif M - c <= 0:
            judge = False
        elif n-c >= (M-c)*2:
            judge = True
        else:
            judge = False
        
    
    print("Yes" if judge else "No")
    
    
    
if __name__ == "__main__":
    main()

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(readline())
    a = list(map(int, readline().split()))
    a_max = max(a)
    a_min = min(a)
    if a_max - a_min > 1:
        return print("No")
    elif a_max == a_min:
        if 1 <= a_min <= n // 2 or a_min == n - 1:
            return print("Yes")
        else:
            return print("No")
    else:
        c_min = a.count(a_min)
        c_max = a.count(a_max)

        if 1 <= a_max - c_min <= c_max // 2:
            return print("Yes")
        else:
            return print("No")


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.buffer.readline




def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))




def main():
	n=II()
	A=LI()


	A.sort()

	a=A[0]
	b=A[-1]

	ans="No"

	if b-a==0:
		if n==a+1 or n>=a*2:
			ans="Yes"

	elif b-a==1:
		aa=A.count(a)
		if a>=aa and 2*a-aa+2<=n:
			ans="Yes"




	print(ans)




















if __name__ == "__main__":
	main()

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
M = max(A)
m = min(A)

if M-m >= 2:
    print('No')
elif M-m == 0:
    
    if M == n-1:
        print('Yes')
    elif M*2 <= n:
        print('Yes')
    else:
        print('No')
else:
    c = M
    x = 0
    for a in A:
        if a == m:
            x += 1
    if x < c and (c-x)*2 <= (n-x):
        print('Yes')
    else:
        print('No')

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
M = max(A)
m = min(A)

if M-m >= 2:
    print('No')
elif M-m == 0:
    
    if M == n-1:
        print('Yes')
    elif M*2 <= n:
        print('Yes')
    else:
        print('No')
else:
    c = M
    x = 0
    for a in A:
        if a == m:
            x += 1
    if x < c and (c-x)*2 <= (n-x):
        print('Yes')
    else:
        print('No')

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
if max(a) - min(a) >= 2:
    print("No")
    exit()
else:
    M = max(a)
    m = min(a)
    if M == m:
        if m <= N / 2 or m == N - 1:
            print("Yes")
        else:
            print("No")
    else:
        if a.count(m) >= M or a.count(M) + 2 * a.count(m) < 2 * M:
            print("No")
        else:
            print("Yes")

Solution (Python):

def solve(A):
    N = len(A)
    p = max(A)
    if p > min(A)+1:
        return False

    cnt = sum(int(a==p) for a in A)

    if p == N-1 and cnt == N:
        return True

    cnt_max = 2*(N-p)
    cnt_min = N-p+1

    return cnt_min <= cnt <= cnt_max

if __name__ == '__main__':
    N = int(input())
    A = tuple(map(int,input().split()))
    print('Yes' if solve(A) else 'No')

Solution (Python):
'''
研究室PCでの解答
'''
import math

import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
dir = [(-1,0),(1,0),(0,-1),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"


def main():
    n = int(ipt())
    a = [int(i) for i in ipt().split()]
    mi = min(a)
    ma = max(a)

    if ma-mi > 1:
        print("No")
    elif ma == mi:
        if 1 <= mi <= n//2 or mi == n-1:
            print("Yes")
        else:
            print("No")
    else:
        nmi = a.count(mi)
        nma = a.count(ma)
        if nmi+1 <= ma <= nmi+nma//2:
            print("Yes")
        else:
            print("No")

    return None

if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
amax=max(a)
amin=min(a)
if amax-amin>=2:
    print("No")
    exit()
elif amax==amin:
    if amax==n-1 or amax*2<=n:
        print("Yes")
        exit()
    else:
        print("No")
        exit()
ac=a.count(amin)
ad=n-ac
if amin-ac<0:
    print("No")
    exit()

if (amax-ac)*2<=ad:
    print("Yes")
else:
    print("No")

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
M = max(a)
m = min(a)
if M > m + 1:
    print("No")
else:
    if M == m:
        if M == N-1:
            print("Yes")
        elif M*2 <= N:
            print("Yes")
        else:
            print("No")

    else:
        
        c = a.count(m)
        if (M - c) * 2 <= N-c and c < M:  
            print("Yes")
        else:
            print("No")

Solution (Python):
n = int(input())
A = list(int(x) for x in input().split())

ma = max(A)
mi = min(A)

if ma - mi > 1:
    print("No")
    exit()

if ma == mi:
    if n >= ma * 2 or n == ma + 1:
        print("Yes")
    else:
        print("No")
    exit()


if A.count(mi) <= mi and A.count(ma) >= (ma - A.count(mi)) * 2:
    print("Yes")
else:
    print("No")

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
from collections import Counter
ca=dict(Counter(a))
if len(ca.keys())>2:
  print('No')
elif len(ca.keys())==1:
  if 2*a[0]<=n or a[0]==n-1:
    print('Yes')
  else:
    print('No')
else:
  c=max(ca.keys())
  d=min(ca.keys())
  
  
  if c!=d+1:
    print('No')
  else:
    tmp=c-ca[d]
    
    if tmp>0 and 2*tmp<=ca[c]:
      print('Yes')
    else:
      print('No')

Solution (Python):
def answer(x):
  print(x)
  exit()

N=int(input())
A=list(map(int,input().split()))
if len(set(A))>=3:
  answer('No')
if len(set(A))==1:
  if N//2>=A[0] or A[0]==N-1:
    answer('Yes')
  else:
    answer('No')
A.sort()
if A[0]+1<A[-1]:
  answer('No')
X=1
for i in range(N-1):
  if A[i]==A[i+1]:
    X+=1
  else:
    break
if X==N-1:
  answer('No')
if X+1<=A[-1]<=X+(N-X)//2:
  answer('Yes')
else:
  answer('No')

Solution (Python):
import sys
import collections

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

N = ini()
A = inl()
A.sort()


def solve():
    K = A[-1]
    if A[0] == K:
        return K == N - 1 or (K >= 1 and 2 * K <= N)
    if A[0] != K - 1:
        return False
    counter = collections.Counter(A)
    d = counter[K - 1]
    if K <= d:
        return False
    if N - d <= 1:
        return False
    return 2 * (K - d) <= N - d


print("Yes" if solve() else "No")

Solution (Python):
def check():
    N = int(input())
    A = list(map(int, input().split()))
    if len(set(A))==1:
        if max(A)==N-1:
            return 'Yes'
    large = max(A)
    small = large-1
    n_s = list(map(lambda x:x-small,A)).count(0)
    if large==n_s:
        if n_s==N:
            return 'Yes'
        return 'No'
    if large<n_s:
        return 'No'
    if 2*(large-n_s)+n_s<=N:
        return 'Yes'
    return 'No'
print(check())

Solution (Python):
n = int(input())
a = list(map(int,input().split()))

if max(a)-min(a) == 0:
    if a[0] == n-1 or 2*a[0] <= n:
        print("Yes")
    else:
        print("No")
elif max(a)-min(a) == 1:
    A = max(a)
    alone = 0
    not_alone = 0
    for i in a:
        if i == A:
            not_alone += 1
        else:
            alone += 1
    if alone < A and 2*(A-alone) <= not_alone:
        print("Yes")
    else:
        print("No")
else:
    print("No")

Solution (Python):
yes="Yes"
no="No"
n=int(input())
a=list(map(int,input().split()))
if max(a)-min(a)>1:exit(print(no))
if a==[a[0]]*n:
  if a[0]==n-1:print(yes)
  elif 2*a[0]<=n:print(yes)
  else:print(no)
else:
  mi=a.count(min(a))
  ma=n-mi
  if mi<max(a) and 2*(max(a)-mi)<=ma:print(yes)
  else:print(no)

Solution (Python):
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def main():
    N = int(input())
    A = list(map(int,input().split()))

    a = max(A)
    if not all(0 <= a - b <= 1 for b in A):
        print('No')
        return
    
    cnt = sum(0 if a == b else 1 for b in A)
    if cnt == 0 and (2*a <= N or a + 1 == N):
        print('Yes')
    elif a == cnt == N:
        print('Yes')
    elif a - cnt > 0 and (a - cnt)*2 <= N - cnt:
        print('Yes')
    else:
        print('No')

if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
a = list(map(int, input().split()))
s = set(a)
if len(s)>2:
    ans = "No"
elif len(s)==2:
    if max(s)-min(s)!=1:
        ans = "No"
    else:
        x = max(s)
        n2 = sum(num==x-1 for num in a)
        if x-n2>0 and n2 + 2*(x-n2) <= n:
            ans = "Yes"
        else:
            ans = "No"
else:
    x = max(s)
    if x==n-1 or 2*x<=n:
        ans = "Yes"
    else:
        ans = "No"
print(ans)

Solution (Python):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(500000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

n=int(input())
a=list(map(int,input().split()))
cnt=Counter(a)
if len(cnt)>2:
    print("No")
    quit()

if len(cnt)==1:
    if n==a[0]+1 or a[0]*2<=n:
        print("Yes")
    else:
        print("No")
    quit()

syo=cnt[min(a)]
dai=cnt[max(a)]
if max(a)-min(a)!=1:
    print("No")
    quit()
maru=max(a)-syo
if maru<=0:
    print("No")
elif maru*2<=dai:
    print("Yes")
else:
    print("No")

Solution (Python):


import math

import itertools
from collections import deque
from collections import Counter




n=int(input())
a=list(map(int,input().split()))
m=min(a)
M=max(a)
if M-m>1:
    print("No")
    exit()
elif M-m==0:
    if a[0]==n-1 or 2*a[0]<=n:
        print("Yes")
    else:
        print("No")
elif M-m==1:
    c=0
    for i in range(n):
        if a[i]==m:
           c+=1
    if 2<=2*(M-c)<=n-c:
        print("Yes")
    else:
        print("No")

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
if max(a) - min(a) >= 2:
    print("No")

elif max(a) - min(a) == 0:
    if a[0] == n-1 or 2*a[0] <= n:
        print("Yes")
    else:
        print("No")

else:
    p = a.count(max(a))
    q = a.count(min(a))
    if q+1 <= max(a) <= q+p//2:
        print("Yes")
    else:
        print("No")

Solution (Python):
import sys
input=sys.stdin.readline
n=int(input())
a=list(map(int,input().split()))
ans=False
aa=a[0]

s1=0
s2=0
check=True
for i in range(n):
    if a[i]==aa:
        s1+=1
    elif a[i]==aa+1:
        s2+=1
    else:
        check=False
        break
if check:
    if s2==0:
        if aa==n-1:
            ans=True
    else:
        if 2<=(aa+1-s1)*2<=s2:
            ans=True
s1=0
s2=0
check=True
for i in range(n):
    if a[i]==aa-1:
        s1+=1
    elif a[i]==aa:
        s2+=1
    else:
        check=False
        break
if check:
    if s2==0:
        pass
    else:
        if 2<=(aa-s1)*2<=s2:
            ans=True

if ans:
    print('Yes')
else:
    print('No')

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))

A.sort()

mn = min(A)
mx = max(A)
if mx - mn > 1:
    print('No')
    exit()

if mn == mx:
    if N == mn + 1 or N >= 2*mn:
        print('Yes')
    else:
        print('No')
    exit()

nsolo = A.count(mn)
ndup = N - nsolo

if nsolo  > mn:
    print('No')
    exit()

if ndup < 2*(mn - nsolo+1):
    print('No')
    exit()

print('Yes')

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
A = sorted(map(int, readline().split()))

def main(A):
    N = len(A)
    if A[0] == A[-1]:
        x = A[0]
        return x == N - 1 or 2 * x <= N
    if A[0] + 1 != A[-1]:
        return False
    a, b = A[0], A[-1]
    full = b
    unique = A.count(a)
    people, color = N - unique, A[-1] - unique
    return color > 0 and 2 * color <= people

print('Yes' if main(A) else 'No')

Solution (Python):
import bisect
n=int(input())
a=list(map(int,input().split()))
s=max(a)-min(a)
if s>=2:
    print("No")
elif s==0:
    if a[0]==n-1 or n//a[0]>=2:
        print("Yes")
    else:
        print("No")
else:
    a.sort()
    x=bisect.bisect_right(a,a[0])
    if x+1<=a[-1]<=x+(n-x)//2 and n-x>=2:
        print("Yes")
    else:
        print("No")

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ma, mi = max(a), min(a)
if ma - mi > 1:
    print("No")
elif ma - mi == 1:
    z, o = 0, 0
    for i in range(n):
        if a[i] == ma - 1:
            z += 1
        else:
            o += 1
    if z < ma and ma - z <= o / 2:
        print("Yes")
    else:
        print("No")
else:
    if ma == n - 1 or ma <= n / 2:
        print("Yes")
    else:
        print("No")

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
m = min(A)
M = max(A)
if M - m > 1:
  print("No")
elif M - m == 0:
  if 1 <= M <= n//2 or m == n-1:
    print("Yes")
  else:
    print("No")
else:
  k = A.count(m)
  if 1 <= M-k <= (n-k)//2:
    print("Yes")
  else:
    print("No")

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
M = max(A); m = min(A)
if M - m > 1:
  print('No')
elif M - m == 1:
  c = A.count(m)
  if 2*M - N <= c < M:
    print('Yes')
  else:
    print('No')
else:
  if m == N-1:
    print('Yes')
  elif N >= 2*m:
    print('Yes')
  else:
    print('No')

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
if max(a)-min(a)>=2:exit(print("No"))
if len(set(a))==1:
  m=a[0]
  if m==n-1 or 2*m<=n:print("Yes")
  else:print("No")
else:
  x=a.count(min(a))
  y=n-x
  if x<max(a) and 2*(max(a)-x)<=y:print("Yes")
  else:print("No")

Solution (Python):
N = int(input())
a = list(map(int, input().split()))

cat = {}
for c in a:
  if c in cat:
    cat[c] += 1
  else:
    cat[c] = 1

min_cat = N
min_cat_cnt = 0
max_cat = 0
max_cat_cnt = 0
for c in cat:
  if c < min_cat:
    min_cat = c
    min_cat_cnt = cat[c]
  if c > max_cat:
    max_cat = c
    max_cat_cnt = cat[c]
    
if max_cat - min_cat == 1 and min_cat_cnt <= min_cat and 2 * min_cat - min_cat_cnt + 2 <= N:
  print("Yes")
elif max_cat == min_cat and 2 * (max_cat) <= N:
  print("Yes")
elif max_cat == min_cat and min_cat_cnt - 1 == min_cat:
  print("Yes")
else:
  print("No")

Solution (Python):
from collections import Counter

N = int(input())
*a, = map(int, input().split())
v = sum(a)
c = Counter(a)

for n in range(N+1)[::-1]:
    if v-(N-1)*n>=0 and (v-(N-1)*n)%N==0:
        k = (v-(N-1)*n)//N
        if (k==0 and n==N) or (k and (N-n)%k==0):
            if c[n+k]==N-n and c[n+k-1]==n:
                print("Yes")
                break
else:
    print("No")

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

x = min(a)
y = max(a)
if y - x > 1:
    print('No')

elif y == x:
    if n == x + 1 or 2 * x <= n:
        print('Yes')
    else:
        print('No')

else:
    x_cnt = a.count(x)
    y_cnt = a.count(y)

    if y > x_cnt and 2 * (y - x_cnt) <= y_cnt:
        print('Yes')
    else:
        print('No')

Solution (Python):
import sys
input = sys.stdin.readline

N = int(input())
a = list(map(int, input().split()))
m, n = max(a), min(a)
if m - n > 1 or m >= N:
    print('No')
elif m - n == 0:
    print('Yes' if n == N - 1 or n * 2 <= N else 'No')
else:
    print('Yes' if 1 <= m - a.count(n) <= a.count(m) // 2 else 'No')

Solution (Python):
from collections import Counter


def cat():
    N = int(input())
    A = list(map(int, input().split()))

    
    if A == [1] * N:
        return True
    if A == [N - 1] * N:
        return True

    
    Aset = set(A)
    if len(Aset) == 1:
        Amax = A[0]
        cnt = N - Amax + 1
        if N <= 2 * cnt - 2:
            return True
        else:
            return False
    elif len(Aset) == 2:
        A.sort()
        Amax = A[-1]
        Amin = A[0]
        if Amin + 1 != Amax:
            return False
        lower = N - Amax + 1
        upper = 2 * lower - 2
        c = Counter(A)
        if lower <= c[Amax] <= upper:
            return True
        else:
            return False
    else:
        return False


if __name__ == "__main__":
    if cat():
        print("Yes")
    else:
        print("No")

Solution (Python):
from collections import Counter

N = int(input())
A = Counter(map(int, input().split()))

if len(A.keys()) >= 3:
    print('No')
    exit()

if len(A.keys()) == 1:
    p = list(A.keys())[0]
    print('Yes' if 2 * p <= N or p == N - 1 else 'No')
    exit()

p1, p2 = sorted(A.keys())
print('Yes' if p2 - p1 == 1 and (A[p1] + 1) <= p2 <= (A[p1] + A[p2] // 2) else 'No')

Solution (Python):
n=int(input())

a=list(map(int,input().split()))
if len(set(a))==1:
    if n>=2*a[0] or n-1==a[0]:print("Yes")
    else:print("No")
elif len(set(a))>=3:print("No")
else:
    s,S=sorted(list(set(a)))
    k=a.count(s)
    if S==s+1 and 2*S-n<=k and S>k:print("Yes")
    else:print("No")

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
a = na()

mx,mxc = 0,0
mn,mnc = 1000000,0
for i in a:
    if mx == i:
        mxc += 1
    if mn == i:
        mnc += 1
    if mx < i:
        mx = i
        mxc = 1
    if mn > i:
        mn = i
        mnc = 1

d = mx-mn


if d > 1:
    print('No')
elif d == 1:
    if mx > mnc and 2*(mx-mnc) <= mxc:
        print('Yes')
    else:
        print('No')
elif d == 0:
    if a[0] == n-1 or 2*a[0] <= n:
        print('Yes')
    else:
        print('No')

Solution (Python):
n = int(input())
A = list(map(int,input().split()))

A.sort()
v = A[-1] - A[0]
if v >= 2:
    print('No')
elif v == 0:
    if A[0]*2 <= n or A[0] == n-1:
        print('Yes')
    else:
        print('No')
else:
    x = A.index(A[-1])
    y = A[0] - x + 1
    if y > 0 and y*2 <= n-x:
        print('Yes')
    else:
        print('No')

Solution (Python):

import sys

YES = "Yes"  
NO = "No"  


def solve(N: int, a: "List[int]"):
    from collections import Counter
    ma, mi = max(a), min(a)
    if ma >= N or mi < 1:
        raise
    if ma - mi > 1:
        return False
    
    if ma == mi:
        return ma == N - 1 or  2 * ma <= N
    c = Counter(a)
    
    if c[ma] % 2 != 0:
        return False
    x = ma - c[ma] // 2
    
    if x == c[mi]:
        return True
    
    return 1 <= x <= c[mi] - 2


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    a = [int(next(tokens)) for _ in range(N)]  
    print(YES if solve(N, a) else NO)

if __name__ == '__main__':
    main()

Solution (Python):

import sys

YES = "Yes"  
NO = "No"  


def solve(N: int, a: "List[int]"):
    from collections import Counter
    ma, mi = max(a), min(a)
    if ma >= N or mi < 1:
        raise
    if ma - mi > 1:
        return False
    
    if ma == mi:
        return ma == N - 1 or  2 * ma <= N
    c = Counter(a)
    
    if c[ma] % 2 != 0:
        return False
    x = ma - c[ma] // 2
    
    if x == c[mi]:
        return True
    
    return 1 <= x <= c[mi] - 2


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    a = [int(next(tokens)) for _ in range(N)]  
    print(YES if solve(N, a) else NO)

if __name__ == '__main__':
    main()

Solution (Python):

import sys

YES = "Yes"  
NO = "No"  


def solve(N: int, a: "List[int]"):
    from collections import Counter
    ma, mi = max(a), min(a)
    if ma >= N or mi < 1:
        raise
    if ma - mi > 1:
        return False
    if ma == mi:
        return ma == N - 1 or  2 * ma <= N
    c = Counter(a)
    
    if c[ma] % 2 != 0:
        return False
    x = ma - c[ma] // 2
    if x < 0:
        return False
    
    if x == c[mi]:
        return True
    
    return x <= c[mi] - 2


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    a = [int(next(tokens)) for _ in range(N)]  
    print(YES if solve(N, a) else NO)

if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
A=list(map(int,input().split()))

A.sort()
if A[-1]-A[0]>1:
    print('No')
else:
    if A[-1]-A[0]==1:
        n=A.count(A[0])
        m=A.count(A[-1])
        kind=A[-1]-n
        if m>=2*kind and kind>0:
            print('Yes')
        else:
            print('No')
    else:
        if A[0]==N-1:
            print('Yes')
        else:
            if N>=2*A[0]:
                print('Yes')
            else:
                print('No')

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
M=max(a)
m=min(a)
if M>m+1:
  print("No")
elif M==m+1:
  cntm=a.count(m)
  cntM=n-cntm
  if cntm>m:
    print("No")
  elif cntm+cntM//2<M:
    print("No")
  else:
    print("Yes")
elif M==m:
  if m<=n//2:
    print("Yes")
  elif m==n-1:
    print("Yes")
  else:
    print("No")

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline

N, *a = map(int, read().split())
a.sort()
if a[0] + 1 < a[-1]:
    print('No')
elif a[0] == a[-1]:
    if a[-1] == N - 1 or 2 * a[-1] <= N:
        print('Yes')
    else:
        print('No')
else:
    b = a.count(a[0])
    c = N - b
    if b < a[-1] and 2 * (a[-1] - b) <= c:
        print('Yes')
    else:
        print('No')