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())
A = list(map(int, input().split()))

res = True
maxvar = max(A)

x = 0
y = 0

for i in range(N):
    if A[i] == maxvar:
        x += 1
    elif A[i] == maxvar - 1:
        y += 1
    else:
        res = False
        break
else:
    if (maxvar - y) * 2 + y > N or y >= maxvar:
        res = False

    if maxvar == N - 1 and y == 0:
        res = True

print('Yes' if res else 'No')

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

res = True
maxvar = max(A)

x = 0
y = 0

for i in range(N):
    if A[i] == maxvar:
        x += 1
    elif A[i] == maxvar - 1:
        y += 1
    else:
        res = False
        break
else:
    if (maxvar - y) * 2 + y > N or (maxvar - y) * 2 <= 0 or y >= maxvar:
        res = False

    if maxvar == N - 1 and y == 0:
        res = True

print('Yes' if res else 'No')

Solution (Python):
import sys
input=lambda: sys.stdin.readline().rstrip()
n=int(input())
A=[int(i) for i in input().split()]
k=len(set(A))
if k==1:
  if A[0]==n-1 or 2*A[0]<=n:
    print("Yes")
  else:
    print("No")
elif k>2:
  print("No")
else:
  ma=min(A)
  Ma=max(A)
  if Ma-ma>1:
    print("No")
  else:
    ct=0
    for a in A:
      if a==ma:
        ct+=1
    if ma+1>ct and (ma+1-ct)*2<=n-ct:
      print("Yes")
    else:
      print("No")

Solution (Python):

N = int(input())
A = [int(i) for i in input().split()]

MAXA = max(A)
MINA = min(A)

if MAXA - MINA >= 2:
    flag = False
elif MAXA - MINA == 0:
    
    flag = True if (A[0] == N - 1) else False
    
    flag |= True if (2 * A[0] <= N) else False
else:
    x = A.count(MINA) 
    y = A.count(MAXA) 
    
    flag = True if (x < MAXA and 2 * (MAXA - x) <= y) else False

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

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

if(max(a) - min(a) > 1):
    print('No')
    exit()
elif(max(a) - min(a) == 0):
    var = max(a)
    if((var==n-1)|(var <= n//2)):
        print('Yes')
    else:
        print('No')
    exit()

max_n = a.count(max(a))
min_n = a.count(min(a))

max_var = min_n + max_n//2
min_var = min_n + 1

if((min(a) >= min_var - 1) & (max(a) <= max_var)):
    print('Yes')
else:
    print('No')

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
a.sort()
if a[-1] - a[0] > 1:
  print("No")
  exit(0)

if a[0] == a[-1]:
  if a[0] == N - 1:
    print("Yes")
    exit(0)
  if N // a[0] >= 2:
    print("Yes")
  else: print("No")
  exit(0)

table = [0] * N
for i in range(N):
  if a[i] == a[0]: table[i] = i + 1
l = max(table) + 1
r = l + table.count(0) // 2

if a[-1] in range(l, r): print("Yes")
else: print("No")

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
a,b=max(l),min(l)
if a-b>1:
    ans="No"
elif a==b:
    if a<=n//2 or a==n-1:
        ans="Yes"
    else:
        ans="No"
else:
    if 2*a-n<=l.count(b)<=b:
        ans="Yes"
    else:
        ans="No"
print(ans)

Solution (Python):
n = int(input())
a = sorted(list(map(int, input().split())))
data = [[-1, 0]]
for x in a:
    if data[-1][0] == x:
        data[-1][1] += 1
    else:
        data.append([x, 1])
data = data[1:]

if len(data) == 1:
    print("Yes" if data[0][0] * 2 <= n or data[0][0] + 1 == n else "No")
elif len(data) == 2:
    if data[0][0] + 1 == data[1][0] and data[0][0] >= data[0][1] and data[1][1] >= 2 * (data[1][0] - data[0][1]):
        print("Yes")
    else:
        print("No")
else:
    print("No")

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

a.sort()
if a[-1] - a[0] >= 2:
    print("No")
elif a[-1] - a[0] == 1:
    m = 0
    for x in a:
        if x == a[0]:
            m += 1
        else:
            break
    if  a[-1]-m <= (n-m)//2 and a[-1]-m > 0:
        print("Yes")
    else:
        print("No")
else:
    if a[0] == n-1 or a[0] <= n//2:
        print("Yes")
    else:
        print("No")

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

d = {}

for ai in a:
    if ai not in d:
        d[ai] = 0
    d[ai] += 1

f = 1

k = list(d.keys())
k.sort()


b = 0

if len(k) > 2:
    f = 0
elif len(k) == 2:
    if k[1] - k[0] != 1:
        f = 0
    b = d[k[0]]


g = k[-1] - b

if g <= 0 or g > d[k[-1]] // 2:
    f = 0

if len(k) == 1 and k[-1] == n - 1:
    f = 1

if f:
    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):
    if max(a)==n-1 or 2*max(a)<=n:
        print('Yes')
    else:
        print('No')
else:
    mi=0
    ami=min(a)
    for i in a:
        if i==ami:
            mi+=1
    ma=n-mi
    if mi<ami+1 and 2*(ami+1-mi)<=ma:
        print('Yes')
    else:
        print('No')

Solution (Python):
n=int(input())
lists=list(map(int,input().split()))
from collections import Counter
a=dict(Counter(lists))
if len(a)==1:
    if n>=lists[0]*2 or lists[0]==n-1:
        print("Yes")
    else:
        print("No")
    
elif len(a)==2:
    P=list(a.keys())
    Q=list(a.values())
    x,y=min(P),max(P) 
    c,d=a[x],a[y]
    if 2*y-c<=n and c>0 and y>c and y-x==1:
        print("Yes")
    else:
        print("No")

    
else:
    print("No")

Solution (Python):
N, *A = map(int, open(0).read().split())
s = set(A)
if max(s)-min(s)>1:
  print('No')
else:
  if len(s)==1:
    if A[0]==N-1:
      print('Yes')
    elif 2*A[0]<=N:
      print('Yes')
    else:
      print('No')
  elif len(s)==2:
    N -= A.count(min(s))
    u = max(s)-A.count(min(s))
    if u<=0:
      print('No')
    elif 2*u<=N:
      print('Yes')
    else:
      print('No')
  else:
    print('No')

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

def main():
    N = int(input())
    a = list(map(int,input().split()))
    
    if (max(a)-min(a) >= 2):
        print("No")
    else:
        count = [0,0]
        t = min(a)
        for num in a:
            if num == t:
                count[0] += 1
            else:
                count[1] += 1
        if (count[0]*count[1] == 0):
            if (2*a[0] <= N or a[0] == N-1):
                print("Yes")
            else:
                print("No")
        else:
            if (count[0] < t+1 and 2*(t+1-count[0]) <= count[1]):
                print("Yes")
            else:
                print("No")

if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
B = []
C = []
for num in A:
    if len(B) >= 3:
        print("No")
        break
    elif num in B: 
        C[B.index(num)] += 1
    else:
        B.append(num)
        C.append(1)


if len(B) >= 3:
    pass
elif len(B) == 1:
    if n == B[0] +1:
        print('Yes')
    elif n//B[0] >= 2:
        print('Yes')
    else:
        print('No')
elif B[0]  == (B[1]+1):
    k = C[1]
    m = B[1]
    if m-k+1 <= 0:
        print('No')
    elif (n-k) % (m-k+1) == 0:
        print('Yes')
    else:
        print('No')
elif B[1] == (B[0]+1):
    k = C[0]
    m = B[0]
    if m-k+1 <= 0:
        print('No')
    elif (n-k) % (m-k+1) == 0:
        print('Yes')
    else:
        print('No')
else:
    print('No')

Solution (Python):
N = int(input())
A = [int(a) for a in input().split()]

A.sort()
flag = True
for a in A:
    if a > A[0]+1:
        flag = False

ans = "No"
if flag:
    cnt = A.count(A[0])
    if cnt == N and (A[0] == N-1 or A[0]*2 <= N):
        ans = "Yes"
    elif cnt == A[0] and cnt < N-1:
        ans = "Yes"
    elif cnt < A[0] and N-cnt >= 2*(A[0]-cnt+1):
        ans = "Yes"
        
print(ans)

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
Ma,ma=max(A),min(A)
b=Ma-ma<=1
if b:
    if Ma==ma:
        b=N==Ma+1 or N//2>=Ma
    else:
        Mc=len(list(a for a in A if a==Ma))
        mc=N-Mc
        b=mc+1<=Ma<=mc+Mc//2
print("YNeos"[not b::2])

Solution (Python):
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True 
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n = inn()
a = inl()
mx = max(a)
mn = min(a)
if mx>mn+1:
    print('No')
elif mx==mn:
    if mx==n-1 or n>=2*mn:
        print('Yes')
    else:
        print('No')
else:
    k = len([x for x in a if x==mn])
    if k<mx and n-k>=2*(mx-k):
        print('Yes')
    else:
        print('No')

Solution (Python):
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = np.array(lr())
diff = A.max() - A.min()
if diff > 1:
    print('No')
    exit()
if diff == 0:
    if A[0] == N-1:
        print('Yes')
    else:
        group = A[0]
        if 2 * group > N:
            print('No')
        else:
            print('Yes')
    exit()
else:
    
    x = np.count_nonzero(A==A.min()) 
    group = A.max()
    if x < group and 2 * (group-x) <= N - x:
        print('Yes')
    else:
        print('No')

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a.sort()
if a[n-1]-a[0]>=2:
  print('No')
  exit()
if a[n-1]==a[0]:
  if a[0]==n-1 or 2*a[0]<=n:
    print('Yes')
  else:
    print('No')
else:
  x=a[0]
  cnt=0
  for i in range(n):
    if a[i]==x:
      cnt+=1
    else:
      break
  if cnt<a[n-1] and 2*(a[n-1]-cnt)<=n-cnt:
    print('Yes')
  else:
    print('No')

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
s = set(a)
if len(s)>=3:
    print('No')
elif len(s)==2:
    
    mx = max(a)
    mn = min(a)
    y = a.count(mx)
    x = a.count(mn)
    if mx-mn>1:
        print('No')
    elif x < mx and 2*(mx-x)<=y:
        print('Yes')
    else:
        print('No')
else:
    if a[0]==n-1:
        print('Yes')
    elif a[0]*2<=n:
        print('Yes')
    else:
        print('No')

Solution (Python):
import sys
N = int(input())
a = list(map(int, input().split()))
m = max(a)
cnt0 = a.count(m)
cnt1 = a.count(m-1)
if cnt0+cnt1 != N:
    print('No')
    sys.exit()
if cnt1+1<=m<=cnt1+cnt0//2:
    print('Yes')
elif cnt1 == 0 and m+1 == cnt0:
    print('Yes')
else:
    print('No')

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

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

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

ls_A = len(set(A))

if ls_A >= 3:
  print("No")
  exit()
  
if max(A) - min(A) >= 2:
  print("No")
  exit()

if len(set(A)) == 1:
  if A[0] == N - 1:
    print("Yes")
    exit()
  if N // A[0] >= 2:
    print("Yes")
  else:
    print("No")
else:
  min_A = min(A)
  cnt_min = 0
  for a in A:
    if a == min_A:
      cnt_min += 1
  N -= cnt_min  
  if max(A) - cnt_min <= 0:
    print("No")
    exit()
  if N // (max(A) - cnt_min) >= 2:
    print("Yes")
  else:
    print("No")

Solution (Python):
N = int(input())
A = sorted(list(map(int,input().split())),reverse=True)
if A[0]==A[-1] and (A[0]==1 or A[0]==N-1):
    print("Yes")
else:
    k = A[0]
    cnt = A.count(k-1)
    n = N-cnt
    k -= cnt
    if k<=0:
        print("No")
    else:
        n -= 2*k
        if n>=0:
            print("Yes")
        else:
            print("No")

Solution (Python):

N=int(input())
A=list(map(int,input().split()))
D={}
for a in A:
  if(a in D):
    D[a]+=1
  else:
    D[a]=1
    
if(max(D)-min(D)>1):
  print("No")
  exit()
  
if(len(D)==2):
  B=max(D)
  if(B-D[B-1]>0 and D[B]>=2*(B-D[B-1])):
    print("Yes")
  else:
    print("No")
  
else:
  B=max(D) 
  if(N>=2*B):
    print("Yes")
    exit()
  
  B=max(D)+1 
  if(B==N):
    print("Yes")
    exit()
  print("No")

Solution (Python):
N = int(input())
A = [int(a) for a in input().split()]
X = min(A)
Y = max(A)
if Y - X > 1:
    print("No")
elif X == Y:
    print("Yes" if X <= N//2 or X == N-1 else "No")
else:
    K = A.count(X)
    print("Yes" if N-K > 1 and K+1 <= X+1 <= K + (N-K)//2 else "No")

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
m = min(a)
M = max(a)
num_m = 0
num_M = 0
if M - m >= 2:
  print("No")
else:
  if m == M:
    if m == n-1:
      print("Yes")
    else:
      if m <= n/2:
        print("Yes")
      else:
        print("No")
  else:
    for i in range(n):
      if a[i] == m:
        num_m += 1
    more = n - num_m
    num_remain = M - num_m
    if 0 < num_remain <= more/2:
      print("Yes")
    else:
      print("No")

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
if len(set(l)) == 1:
  if l[0] <= n//2 or l[0] == n-1:
    print('Yes')
  else:
    print('No')
elif len(set(l)) > 2 or max(l)-min(l) > 1:
  print('No')
else:
  ma = max(l)
  mi = ma - 1
  mac = l.count(ma)
  mic = n - mac
  if mic <= mi <= mic-1+mac//2:
    print('Yes')
  else:
    print('No')

Solution (Python):
n=int(input())
L=sorted(list(map(int,input().split())))
if L[-1]-L[0]>1:
  s='No'
elif L[-1]-L[0]==1:
  c=L.count(L[0])
  s = ('Yes'if c<L[-1] and 2*(L[-1]-c)<=n-c else 'No')
else:
  s = ('Yes'if L[0]==n-1 or 2*L[-1]<=n else 'No')
print(s)

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

a = max(A)
b = min(A)






ok = 0

if a - b == 1:
    C = [0, 0]
    for i in range(n):
        C[A[i] - b] += 1
    if a - C[0] > 0 and C[1] // 2 >= a - C[0]:
        ok = 1
elif a == b:
    if n == a + 1:
        ok = 1   
    if n // 2 >= a:
        ok = 1

if ok == 1:
    print("Yes")
else:
    print("No")

Solution (Python):


import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
A = LIST()

li = [{1: N}]
for i in range(1, N-1):
    li.append({i: i, i+1: min(N-i+max(N-i-2, 0), N)})
li.append({N-1: N})

C = Counter(A)
for d in li:
    cnt = 0
    for k, v in d.items():
        if C[k] <= v:
            cnt += C[k]
        else:
            break
    else:
        if cnt == N:
            Yes()
            exit()
No()

Solution (Python):
N, *A = map(int, open(0).read().split())

def solve():
    A.sort()

    if A[-1] - A[0] >= 2:
        return False

    if A[-1] - A[0] == 0:
        return (A[0] == N - 1) or (2 * A[-1] <= N)

    M = A.count(A[0])
    return (M < A[-1]) and (2 * (A[-1] - M) <= N - M)

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

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

ma = max(a)
mi = min(a)
if ma-mi >= 2:
    print('No')
elif ma == mi:
    if a[0] <= N//2 or a[0] == N-1:
        print('Yes')
    else:
        print('No')
else:
    maxnum = a.count(ma)
    minnum = a.count(mi)
    
    
    if minnum + 1 <= ma <= minnum + maxnum//2:
        print('Yes')
    else:
        print('No')

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
c = list(Counter(a).items())
c.sort()
if len(c) > 2:
    print("No")
    exit()
if len(c) == 1:
    if c[0][0] <= n//2 or c[0][0] == n-1:
        print("Yes")
    else:
        print("No")
    exit()
if c[1][0]-c[0][0] != 1:
    print("No")
    exit()
if c[0][1] > c[0][0] or (c[1][0]-c[0][1])*2>c[1][1]:
    print("No")
else:
    print("Yes")

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
a.sort()
k = a.count(a[0])
if a[-1] - a[0] > 1:
    print('No')
elif a[-1] == a[0]:
    if N == a[0] + 1:
        print('Yes')
    elif N//2 >= a[0]:
        print('Yes')
    else:
        print('No')
elif a[-1] < k + 1:
    print('No')
elif a[-1] > (N-k)//2 + k:
    print('No')
else:
    print('Yes')

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
max_a = max(a)
min_a = min(a)
if min_a == max_a:
  if min_a == n-1 or min_a <= n // 2:
    print("Yes")
  else:
    print("No")
elif min_a + 1 == max_a:
  x = a.count(min_a)
  y = a.count(max_a)
  if x < max_a and (max_a - x) * 2 <= y:
    print("Yes")
  else:
    print("No")
else:
  print("No")

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = LI()
    M,m = max(a),min(a)
    if M-m > 1:
        print("No")
        return
    if M == m:
        if m == n-1:
            print("Yes")
        elif 2*m <= n:
            print("Yes")
        else:
            print("No")
        return
    b = a.count(m)
    a = a.count(M)
    k = M-b
    if b+1 <= M and 2*k <= a:
        print("Yes")
    else:
        print("No")
    return


if __name__ == "__main__":
    solve()

Solution (Python):
def examA():
    Alpha = [chr(i) for i in range(97, 97+26)]
    S = SI(); N = len(S)
    ans = -1
    for i in range(N):
        for s in Alpha:
            cur = [s for s in S]
            for l in range(i):
                for k in range(N-1):
                    if cur[k+1]==s:
                        cur[k]=s
 
            if len(Counter(cur[:N-i]))==1:
                ans = i
                break
        if ans>=0:
            break
    print(ans)
    return

def examB():
    N = I()
    A = LI()
    d = defaultdict(int)
    for a in A:
        d[a] +=1
    l = min(d); r = max(d)
    ans = "Yes"
    if l>=N:
        ans = "No"
    if r-l>1:
        ans = "No"
    if d[l]>l:
        ans = "No"
    if (r-d[l])*2+d[l]>N:
        ans = "No"
    if r==l and (N-l==1 or N//2>=l):
        ans = "Yes"
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examB()

Solution (Python):
N = int(input())
hat = list(map(int, input().split()))
hat.sort()
min, max = hat[0], hat[N - 1]
count_min = 0
count_max = N
if max - min > 1:
    print("No")
elif max == min:
    if max <= N // 2 or max == N-1:
        print("Yes")
    else:
        print("No")
else:
    for i in range(N):
        if hat[i] < max:
            count_min += 1
            count_max -= 1
        else:
            break
    
    if count_min < max <= count_min + count_max // 2:
        print("Yes")
    else:
        print("No")

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


b = [0 for i in range(n+1)]
for i in range(n):
    b[a[i]]+=1
    b[a[i]+1]+=1

for i in range(1,n+1):
    
    if b[i]==n:
        
        unique = 0
        for j in range(n):
            if a[j]==i-1:
                unique+=1
        
        common = n-unique
        
        if common==1:
            break
        
        elif common==0:
            if unique==i:
                print('Yes')
                exit()
        
        elif unique+1<=i<=common//2+unique:
            print('Yes')
            exit()
print('No')

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
K, L = min(a), max(a)
s = a.count(K)

if L-K>1:
    print("No")
    quit()
if L==K:
    print("Yes" if K==n-1 or K<=n//2 else "No")
else:
    t = n-s
    print("Yes" if L-s>0 and L-s<=t//2 else "No")

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=list(map(int,input().split()))
    m=min(A); M=max(A)

    
    
    
    
    if(M-m>1):
        print("No")
        return

    
    if(m==M):
        if(m==n-1): 
            print("Yes")
            return
        elif(m<=n//2): 
            print("Yes")
            return
        else:
            print("No")
            return

    
    s=A.count(m) 
    t=A.count(M) 
    
    if(s+1<=M<=s+t//2):
        print("Yes")
    else:
        print("No")

resolve()

Solution (Python):
from collections import Counter

N = int(input())
a = [int(i) for i in input().split()]

def solve() :
    c = Counter(a)
    if len(c.keys()) > 2 :
        return 'No'
    
    if len(c.keys()) == 2 :
        if max(c.keys()) - min(c.keys()) != 1 :
            return 'No'
            
        mk, Mk = min(c.keys()), max(c.keys())
        mv, Mv = c[mk], c[Mk]
        n = N - mv
        m = Mk - mv
        if 0 < m * 2 <= n :
            return 'Yes'
        else :
            return 'No'
    else :
        n = N
        m = list(c.keys())[0]
        if m * 2 <= n or m == n - 1 :
            return 'Yes'
        else :
            return 'No' 

print(solve())

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
a.sort()
mx = max(a)
mn = min(a)
c = mx + 0
b = 0
s = 0
if mx == mn and (mx != N - 1):
  mn -= 1
if mx >= N:
  print("No")
  exit(0)
for i in range(N):
  if abs(a[i] - mx) > 1:
    print("No")
    exit(0)
  if a[i] == mn:
    c = a[i] + 1
    s += 1
  else:
    b += 1
if s >= c and (b != 0 and s != 0):
  print("No")
  exit(0)
elif s > c:
  print("No")
  exit(0)
if b == 1:
  print("No")
  exit(0)
if b // 2 < c - s:
  print("No")
else: print("Yes")

Solution (Python):
n, *a = map(int, open(0).read().split())
M = max(a)
mcnt = a.count(M - 1)
Mcnt = a.count(M)
if Mcnt + mcnt == n:
	if mcnt == 0:
		if 2 * M <= n or M == n - 1:
			print("Yes")
			exit()
	else:
		if 0 < 2 * (M - mcnt) <= n - mcnt:
			print("Yes")
			exit()
print("No")

Solution (Python):

def solve():
    n = int(input())
    a = tuple(map(int, input().split()))

    
    
    

    M = max(a)
    m = min(a)

    delta = M - m
    

    if delta > 1:
        return False
        
    elif delta == 1:
        
        
        

        x = a.count(m)
        

        y = n - x
        
        

        if x >= M:
            return False
            
            

        if (y - (M - x) * 2) not in {0, 1}:
            return False
            
            
            

        return True

    else:
        
        if all(a_ == n - 1 for a_ in a):
            return True
            

        if all(a_ * 2 <= n for a_ in a):
            return True
            
            
            

        return False

    return True


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

Solution (Python):
def main():
    from collections import Counter as ct
    n=int(input())
    a=ct(list(map(int,input().split())))
    if len(a.keys()) not in [1,2]:
        print("No")
        return 0
    if len(a.keys())==1:
        a=list(a.items())
        if a==[(n-1,n)]:
            print("Yes")
            return 0
        if 2*a[0][0]<=a[0][1]:
            print("Yes")
        else:
            print("No")
        return 0
    i=min(a.keys())
    iv=a[i]
    j=max(a.keys())
    jv=a[j]
    if j-i!=1:
        print("No")
        return 0
    if iv>i:
        print("No")
        return 0
    k=2*(i-iv+1)
    if jv>=k:
        print("Yes")
    else:
        print("No")
main()

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A):
    A.sort()
    d = A[-1] - A[0]
    if d >= 2:
        return 'No'
    elif d == 0:
        if A[0]*2 <= N or A[0] == N-1:
            return 'Yes'
        else:
            return 'No'
    else:
        K = max(A)
        C = Counter(A)
        y = C[K]
        x = C[K-1]
        if x < K and 2*(K-x) <= y:
            return 'Yes'
        else:
            return 'No'

def main():
    N = read_int()
    A = read_int_n()
    print(slv(N, A))


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter,defaultdict,deque
import sys
import bisect
import math
import itertools
import string
import queue
import copy
from heapq import heappop, heappush
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

def inp(): 
    return int(input())
def inpm(): 
    return map(int,input().split())
def inpl(): 
    return list(map(int, input().split()))
def inpls(): 
    return list(input().split())
def inplm(n): 
    return list(int(input()) for _ in range(n))
def inpll(n): 
    return sorted([list(map(int, input().split())) for _ in range(n)])
def sortx(x,n,k):
    if k == 0:x.sort(key=lambda y:y[1,n])
    else:x.sort(reversed=True, key=lambda y:y[1,n])
def graph():
    n=inp()
    g=[[] for _ in range(n)]
    for i in range(n):
        a=inp()
        a-=1
        g[i].append(a)
        g[a].append(i)
    return n,g
def graphm():
    n,m=inpm()
    g=[[] for _ in range(n)]
    for _ in range(m):
        a,b,w=inpm()
        a-=1
        b-=1
        g[a].append((b,w))
        g[b].append((a,w))
    return n,m,g

def dijkstra(s,n,g): 
    s -= 1
    que = []
    d = [10**15 for _ in range(n)]
    d[s] = 0
    heappush( que,(0,s) )
    while len(que)>0:
        p =  heappop(que)
        v = p[1]
        if d[v] < p[0]:
            continue
        for i in range(len(g[v])):
            edge = g[v][i]
            if d[edge[0]] > d[v] + edge[1]:
                d[edge[0]] = d[v] + edge[1]
                heappush( que,(d[edge[0]] , edge[0]) )
    return d

def main():
    n=inp()
    a=inpl()
    a.sort()
    if a[n-1]-a[0]>1 or a[n-1]>=n or a[0]<1:
        print("No")
        return
    cnt=1
    for i in range(n-1):
        if a[i+1]==a[i]:
            cnt+=1
        else:
            break
    if cnt==n:
        if a[0]==n-1 or n//a[n-1]>=2:
            print("Yes")
            return
        else:
            print("No")
            return
    if cnt>a[0]:
        print("No")
        return
    if (a[n-1]-(n%a[n-1]))>cnt:        
        print("No")
        return
    print("Yes")    


if __name__ == "__main__":
    main()

Solution (Python):

import sys
input = sys.stdin.buffer.readline

from collections import defaultdict

def getlist():
	return list(map(int, input().split()))


def main():
	N = int(input())
	D = defaultdict(int)
	a = getlist()
	for i in a:
		D[i] += 1
	if len(D) >= 3:
		print("No")
	elif len(D) == 1:
		if a[i] * 2 <= N:
			print("Yes")
		elif a[i] + 1 == N:
			print("Yes")
		else:
			print("No")
	else:
		L = []
		for i in D:
			L.append([i, D[i]])
		L = sorted(L)
		if L[0][0] + 1 != L[1][0]:
			print("No")
		else:
			x = L[0][1]
			y = L[1][1]
			A = L[0][0]
			if y < (A - x + 1) * 2 or A - x + 1 <= 0:
				print("No")
			else:
				print("Yes")


if __name__ == '__main__':
	main()

Solution (Python):
N = int(input())
a = list(map(int,input().split()))
a.sort()
if a[0] == a[-1]:
    if a[0] == N-1 or a[0] <= N//2:
        print("Yes")
    else:
        print("No")
    exit()

if a[0] < a[-1]-1:
    print("No")
    exit()

o = 0
t = 0
for i in range(N):
    if a[i] != a[0]:
        o = i
        t = N-i
        break

if t//2 + o >= a[-1] > o:
    print("Yes")
else:
    print("No")

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

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

c = Counter(a)
if len(c.most_common()) > 2:
    print('No')
elif len(c.most_common()) == 1:
    if c.most_common()[0][0] == N-1:
        print('Yes')
    else:
        if c.most_common()[0][0] * 2 <= N:
            print('Yes')
        else:
            print('No')
else:
    x, nx = c.most_common()[0]
    y, ny = c.most_common()[1]
    if x > y:
        x, y = y, x
        nx, ny = ny, nx
    if y - x != 1:
        print('No')
    else:
        if 1 <= y - nx <= ny//2:
            print('Yes')
        else:
            print('No')

Solution (Python):
n = int(input())
xs = [int(x) for x in input().split()]

def solve(n, xs):
    x_min = min(xs)
    x_max = max(xs)

    if x_max - x_min > 1:
        return "No"

    if x_max == x_min:
        if n == x_max+1:
            return "Yes"
        elif x_max*2 <= n:
            return "Yes"
        else:
            return "No"

    solo_num = sum(1 for x in xs if x==x_min)

    if solo_num >= x_max:
        return "No"

    dup_color = x_max - solo_num
    dup_cat = sum(1 for x in xs if x==x_max)

    if dup_cat < dup_color*2:
        return "No"

    return "Yes"

print(solve(n, xs))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
mi = min(a)
ma = max(a)
if ma - mi >= 2:
  print('No')
elif ma - mi == 1:
  alone = a.count(mi)
  if alone < ma and (ma - alone) * 2 <= n - alone:
    print('Yes')
  else:
    print('No')
else:
  if mi * 2 <= n or mi == n-1:
    print('Yes')
  else:
    print('No')