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()))


dic = {}

for i in a:

    if i not in dic:
        dic[i] = 1
    else:
        dic[i] += 1

if len(dic) > 2:
    print ("No")

else:
    mi = float("inf")
    ma = -1

    for i in dic:
        mi = min(mi,i)
        ma = max(ma,i)

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

    elif ma - mi == 1:
        

        if dic[mi] + dic[ma] // 2 == ma and dic[ma] > 1:
            print ("Yes")
        else:
            print ("No")

    else:

        if ma == N-1 or N // 2 >= ma:
            print ("Yes")
        else:
            print ("No")

Solution (Python):
n,*a=list(map(int,open(0).read().split()))
b,c=max(a),min(a)
if b-c==1:
    d,e=a.count(b),a.count(c)
    if e<b and 2*(b-e)<=d:print("Yes")
    else:print("No")
elif b-c==0:
    if a[0]==n-1 or a[0]*2<=n:print("Yes")
    else:print("No")
else:print("No")

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

max_a = max(a)
min_a = min(a)
if max_a - min_a >= 2:
    print("No")
    exit()

if max_a == min_a:
    if max_a == n - 1:
        print("Yes")
    elif max_a <= n // 2:
        print("Yes")
    else:
        print("No")
    exit()

nokori = min_a + 1 - a.count(min_a)
if nokori <= 0:
    print("No")
elif a.count(max_a) // nokori <= 1:
    print("No")
else:
    print("Yes")

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
maxa = max(a)
mina = min(a)
if maxa - mina == 0:
    if maxa <= n // 2 or maxa == n - 1:
        print("Yes")
    else:
        print("No")
elif maxa - mina == 1:
    loneCatNum = len([x for x in a if x == mina])
    if loneCatNum + 1 <= maxa and maxa <= (n + loneCatNum) / 2:
        print("Yes")
    else:
        print("No")
else:
    print("No")

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=sorted(a)
c=0
if(b[n-1]-b[0]>1):
    print('No')
elif(b[n-1]-b[0]==0):
    if(1<=b[0]<=n//2 or b[0]==n-1):
        print('Yes')
    else:
        print('No')
else:
    for i in range(n-1):
        if(b[i]<b[i+1]):
            c=i
            break
    if(c==n-2):
        print('No')
    elif(c+1<=b[0]<=c+(n-c-1)//2):
        print('Yes')        
    else:
        print('No')

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N = int(input())
L = list(map(int, input().split()))
ans = "Yes"

L_max = max(L)
L_min = min(L)

if L_max - L_min >= 2:
    ans = "No"
elif L_max == L_min:
    if not(N >= L[0]*2 or L[0] == N-1):
        ans = "No"
else:
    num_alone = L.count(L_min)
    not_alone_L = [v-num_alone for v in L if v == L_max]
    if not_alone_L[0] <= 0 or len(not_alone_L) < not_alone_L[0]*2:
        ans = "No"
print(ans)

Solution (Python):
n,a=open(0)
n=int(n)
a=sorted(map(int,a.split()))
if a[0]==a[-1]:
  if n-1==a[0]or n//a[0]>=2:
    print('Yes')
  else:
    print('No')
elif a[-1]-1==a[0]:
  x=a.count(a[0])
  if x<a[-1]and(n-x)//(a[-1]-x)>=2:
    print('Yes')
  else:
    print('No')
else:
  print('No')

Solution (Python):
n,a=open(0)
n=int(n)
a=sorted(map(int,a.split()))
if a[0]==a[-1]:
  if n-1==a[0]or n//a[0]>=2:
    print('Yes')
  else:
    print('No')
elif a[-1]-1==a[0]:
  x=a.count(a[0])
  if x<a[-1]and(n-x)//(a[-1]-x)>=2:
    print('Yes')
  else:
    print('No')
else:
  print('No')

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

from collections import Counter

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

C = Counter(A)
if len(C) == 1:
    X = A[0]
    if N >= 2*X or N == X + 1:
        ans = "Yes"
    else:
        ans = "No"
elif len(C) == 2:
    Xs = sorted(C.keys())
    if Xs[0] + 1 != Xs[1]:
        ans = "No"
    else:
        X = Xs[0]
        if C[X] <= X and C[X+1] >= 2*(X-C[X]+1):
            ans = "Yes"
        else:
            ans = "No"
else:
    ans = "No"

print(ans)

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:
    count=0
    for i in range(N):
        if a[i]==N-1 or a[i]<=N//2:
            count+=1
    if count==N:
        print("Yes")
    else:
        print("No")
else:
    q=0
    p=0
    for i in range(N):
        if a[i]==M:
            q+=1
        elif a[i]==m:
            p+=1
    if p+q//2 >= max(a) and max(a)>=p+1:
        print("Yes")
    else:
        print("No")

Solution (Python):
import collections
N = int(input())
A = [int(_) for _ in input().split()]
c = collections.Counter(A)
l = min(c)
r = max(c)
if r != l + 1:
    print('Yes' if r == l == N - 1 or r == l <= N // 2 else 'No')
    exit()
cl = c[l]
cr = c[r]
print('Yes' if cl + 1 <= r <= cl + cr // 2 else 'No')

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(a)
b.sort()
if b[n-1]-b[0] >=2:
    print("No")
else:
    b = sum(a)%n
    c = int((sum(a)-b)/n)
    if b == 0:
        if 2*c > n and n != c+1:
            print("No")
        else:
            print("Yes")
    else:
        if b == 1:
            print("No")
        else:
            if n - b/2 >= c+1 and n <= c+b:
                print("Yes")
            else:
                print("No")

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(a)
b.sort()
if b[n-1]-b[0] >=2:
    print("No")
else:
    b = sum(a)%n
    c = int((sum(a)-b)/n)
    if b == 0:
        if 2*c > n and n != c+1:
            print("No")
        else:
            print("Yes")
    else:
        if b == 1:
            print("No")
        else:
            if n - b/2 >= c+1 and n <= c+b:
                print("Yes")
            else:
                print("No")

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
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)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007



n = I()
L = LI()
A = sorted(list(set(L)))
if len(A) == 1:
    if 1 <= A[0] <= n // 2 or A[0] == n - 1:
        print('Yes')
    else:
        print('No')
elif len(A) == 2 and A[0] + 1 == A[1]:
    cnt = Counter(L)
    min_type_num = cnt[min(L)]  + 1
    max_type_num = cnt[min(L)] + cnt[max(L)] // 2
    if min_type_num <= max(L) <= max_type_num:
        print('Yes')
    else:
        print('No')
else:
    print('No')

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
from collections import Counter
c=sorted(Counter(A).most_common(),reverse=True)
r=0
if len(c)==2 and c[0][0]==c[1][0]+1 and 0<c[0][0]-c[1][1]<=c[0][1]//2:
    r=1
elif len(c)==1 and (c[0][0]==c[0][1]-1 or c[0][0]<=c[0][1]//2):
    r=1
print(['No','Yes'][r])

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

def solve():
    if max(a)-min(a) > 1:
        return False
    if all(i==a[0] for i in a):
        if a[0] == n-1 or a[0] <= n//2:
            return True
        else:
            return False
    color = [0]*n
    unique = min(a)
    for i, ai in enumerate(a):
        if ai == unique:
            color[i] = 1
    s = sum(color)
    can = (n-s)//2

    for i, ai in enumerate(a):
        if ai == unique:
            if not (s <= ai <= s-1+can):
                return False
        else:
            if not (s+1 <= ai <= s+can):
                return False
    return True

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

Solution (Python):
n=input()
m=map(int,input().split())
a=[int(i) for i in m]
d=max(a)-min(a)
if d>1:
    ans=False
elif d==0:
    if (max(a)==len(a)-1 or max(a)*2<=len(a)):
        ans=True
    else:
        ans=False
else:
    max_num=a.count(max(a))
    min_num=a.count(min(a))
    dif=max(a)-min_num
    if (min_num<max(a) and dif*2<=max_num):
            ans=True
    else:
        ans=False
        
if ans:
    print("Yes")
else:
    print("No")

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
import collections
l1 = collections.Counter(a).most_common()
l1.sort()
if len(l1) > 2:
    print("No")
elif len(l1) == 1:
    if l1[0][0] * 2 <= n or l1[0][0] == n - 1:
        print("Yes")
    else:
        print("No")
else:
    if l1[1][0] - l1[0][0] > 1:
        print("No")
    elif l1[1][1] >= (l1[1][0] - l1[0][1]) * 2 and l1[1][0] > l1[0][1]:
        print("Yes")
    else:
        print("No")

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
L = len(set(a))
if L ==1:
  if a[0] < n//2+1 or a[0]==n-1:
    print('Yes')
  else:
    print('No')
elif L == 2:
  M,m =max(a),min(a)
  m_c =a.count(m)
  if M == m+1 and m_c <= m and n-m_c >= 2*(M-m_c):
    print('Yes')
  else:
    print('No')
else:
  print('No')

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

Solution (Python):
from bisect import bisect_left

N = int(input())
A = sorted(list(map(int, input().split())))
flag = False
if A[-1] - A[0] == 0:
    a = A[0]
    if N == a + 1 or N >= 2 * a:
        flag = True
elif A[-1] - A[0] == 1:
    c = bisect_left(A, A[-1])
    if A[-1] > c and 2 * A[-1] - c <= N:
        flag = True
print("Yes" if flag else "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:
  if a[0] == n-1 or a[0]*2 <= n:
    print("Yes")
  else:
    print("No")
else:
  alone = a.count(m)
  left = n - alone
  if alone >= M:
    print("No")
  elif (M - alone)*2 <= left:
    print("Yes")
  else:
    print("No")

Solution (Python):
import sys
def printans(t):
    print(t)
    sys.exit()

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

if all([a==A[0] for a in A]):
    shurui=A[0]
    if shurui==N-1 or shurui*2<=N:
        printans("Yes")
    else:printans("No")
else:
    maxshurui = max(A)
    if all([a in (maxshurui,maxshurui-1) for a in A]):
        unique = A.count(maxshurui-1)
        shurui = maxshurui-unique
        if shurui<=0:
            printans("No")
        if shurui*2<=N-unique:
            printans("Yes")
        else:printans("No")
    else:printans("No")

Solution (Python):
import sys

n = int(input())
a = list(map(int,input().split()))
mi,ma = 10 ** 6,0
cnt = [0] * n
for v in a:
    cnt[v] += 1
    mi = min(mi,v)
    ma = max(ma,v)
if ma - mi >= 2:
    print("No")
    sys.exit()
if mi != ma:
    if cnt[mi] == ma or (n - cnt[mi]) // (ma - cnt[mi]) < 2:
        print("No")
    else:
        print("Yes")
else:
    if mi == n - 1 or mi * 2 <= n:
        print("Yes")
    else:
        print("No")

Solution (Python):
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string



def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7


input=sys.stdin.readline
show_flg=False


n=I()
a=LI()
m,M=min(a),max(a)
n_m=a.count(m)
n_M=a.count(M)

if m+1<M:
    ans=1
elif m+1==M:
    if n_M//2+n_m>=m+1>=n_m+1:
        ans=0
    else:
        ans=1
else:
    if m==n-1 or m<=n//2:
        ans=0
    else:
        ans=1

print(YN[ans])

Solution (Python):
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))


def solve(N, A):
    ma = max(A)
    mi = min(A)
    if ma == mi:
        
        if ma == N - 1:
            return True
        
        return ma * 2 <= N
    if ma - mi == 1:
        A.sort()
        mi_count = bisect.bisect_left(A, ma)
        
        return 0 < (ma - mi_count) * 2 <= N - mi_count
    return False


ok = solve(N, A)
if ok:
    print('Yes')
else:
    print('No')

Solution (Python):



import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
a.sort()
low,high = a[0],a[-1]
if high - low >= 2:
    print('No')
    quit()
if high - low == 1:
    x,y = 0,0
    for i in range(n):
        if a[i] == high:
            y += 1
        else:
            x += 1
    if x < high and 2*(high-x) <= y:
        print('Yes')
    else:
        print('No')
else:
    for i in range(n):
        if a[i] != n-1:
            break
    else:
        print('Yes')
        quit()
    for i in range(n):
        if 2*a[i] > n:
            print('No')
            break
    else:
        print('Yes')

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        for _ in range(num): return []
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(map(int, input().split())) for _ in range(N)]
        return map(list,zip(*read_all))



N = I()
a = III()

small = a[0]
large = a[0]

for i in range(1,N):
    if a[i]>small:
        large = a[i]
        break
    if a[i]<small:
        large = small
        small = a[i]
        break

if small != large:
    if large!=small+1:
        print('No')
        exit()
    small_num = 0
    large_num = 0
    for i in range(N):
        if a[i]==small:
            small_num += 1
        else:
            large_num += 1
    num = large_num
    val = large-small_num
    if 2*val<=num and val>=1:
        print('Yes')
    else:
        print('No')
else:
    num = N
    val = large
    if val+1==num:
        print('Yes')
    elif 2*val<=num:
        print('Yes')
    else:
        print('No')

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

minColor = min(A)
maxColor = max(A)

if minColor == maxColor:
    if minColor == N - 1:  
        print('Yes')
    elif minColor * 2 <= N:  
        print('Yes')
    else:
        print('No')
    exit()

if minColor + 1 != maxColor:
    print('No')
    exit()

minCnt = 0
maxCnt = 0

for a in A:
    if a == minColor:
        minCnt += 1
    elif a == maxColor:
        maxCnt += 1
    else:
        print('No')
        exit()





if (maxColor - minCnt) * 2 <= maxCnt and maxColor >= minCnt + 1:
    print('Yes')
else:
    print('No')

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(pow(10, 6))


def main():
    n = int(input())
    a = list(sorted(map(int, input().split())))
    
    if a[0] == a[-1]:
        if (a[0] == n-1 or a[0]*2 <= n):
            print("Yes")
        else:
            print("No")
    elif a[-1] - a[0] > 1:
        print("No")
    else:
        minc = 0
        for _a in a:
            if _a == a[0]:
                minc += 1
            else:
                break
        if minc <= a[0] and n-minc>=2*(a[-1]-minc):
            print("Yes")
        else:
            print("No")


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
Amax, Amin = max(A), min(A)
if Amax - Amin > 1:
  print('No')
elif Amax - Amin == 1:
  solo = A.count(Amin)
  print('Yes' if 0 < Amax - solo <= (N - solo) // 2 else 'No')
else:
  print('Yes' if (Amax == N - 1) or (Amax <= N // 2) else 'No')

Solution (Python):
N=int(input())
arr=list(map(int,input().split()))
min_,max_=min(arr),max(arr)
ans=None

if max_-min_ > 1:
    ans=False
elif max_-min_ == 1:
    """
    A=max_
    y=len([a for a in arr if a==max_])
    x=N-y
    nc=A-x
    ans=(A-x)>0 and y >=2*(A-x)
    """
    
    y=len([a for a in arr if a==max_])
    x=N-y
    nc=max_-x
    ans=nc>0 and y>=2*nc

else:
    ans=N>=2*min_ or min_+1==N

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

Solution (Python):
N=int(input())
arr=list(map(int,input().split()))
min_,max_=min(arr),max(arr)
ans=None

if max_-min_ > 1:
    ans=False
elif max_-min_ == 1:
    A=max_
    y=len([a for a in arr if a==max_])
    x=N-y
    nc=A-x
    ans=(A-x)>0 and y >=2*(A-x)
else:
    ans=N>=2*min_ or min_+1==N

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

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
min_,max_=min(A),max(A)

def f(n,k):
    return n%k==0 if k>0 else False

ans=None
if max_-min_ > 1:
    ans=False
elif max_-min_ == 1:
    k=max_
    nk=len([a for a in A if a==max_])
    nk1=N-nk
    ans=f(nk,k-nk1)
else:
    ans=f(N,min_) or f(N,min_+1)

if ans is True:
    print("Yes")
else:
    print("No")

Solution (Python):
N=int(input())
*A, = map(int,input().split())
B = {}
for a in A: B[a] = B.get(a,0)+1
m = min(B)
M = max(B)
if m+1<M:
    print("No")
    exit()
def check(n):
    b0 = B.setdefault(n-1,0)
    b1 = B.setdefault(n,0)
    if b1==0:
        return b0==n
    else:
        return 1<= n-b0 <= b1//2
if m+1==M:
    ans = check(M)
else:
    ans = check(m) or check(m+1)
print("Yes" if ans else "No")

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

from collections import Counter

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

x = min(A)
y = max(A)

if x == y:
    
    bl = (x <= N//2) or (x == N-1)
elif y != x + 1:
    bl = False
else:
    a = A.count(x)
    b = A.count(y)
    
    
    
    c = y - a
    bl = (1 <= c <= b//2)

answer = 'Yes' if bl else 'No'
print(answer)

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 - 1:
        print('Yes')
    elif 2 * a[0] <= N:
        print('Yes')
    else:
        print('No')
else:
    al = 0
    na = 0
    for i in range(N):
        if a[i] == ma - 1:
            al += 1
        else:
            na += 1

    if al < ma and 2 * (ma - al) <= na:
        print('Yes')
    else:
        print('No')

Solution (Python):
n=int(input())
arr=list(map(int,input().split()))
amax=max(arr)
amin=min(arr)
if amax-amin>=2:
  print('No')
elif amax-amin==0:
  flag=False
  for i in range(n):
    if arr[i]!=n-1:
      break
  else:
    flag=True
  for i in range(n):
    if 2*arr[i]>n:
      break
  else:
    flag=True
  if flag==True:
    print('Yes')
  else:
    print('No')
elif amax-amin==1:
  cnt1=0
  cnt2=0
  for i in range(n):
    if arr[i]==amax-1:
      cnt1+=1
    else:
      cnt2+=1
  if cnt1<amax and 2*(amax-cnt1)<=cnt2:
    print('Yes')
  else:
    print('No')

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




L = min(a)
R = max(a)
ans = (
    'Yes' if R - L == 0 and (N >= 2 * L or N == L + 1) or
    R - L == 1 and 2 * (L + 1) - N <= sum(x == L for x in a) <= L else
    'No'
)

print(ans)

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

minA, maxA = min(As), max(As)
diff = maxA - minA
if diff > 1:
    print('No')
elif diff == 1:
    numMinA = As.count(minA)
    if numMinA+1 <= maxA <= numMinA+(N-numMinA)//2:
        print('Yes')
    else:
        print('No')
else:
    if 2*maxA <= N or maxA == N-1:
        print('Yes')
    else:
        print('No')

Solution (Python):
from collections import Counter


def main():
    N = int(input())
    A = list(map(int, input().split()))
    C = Counter(A)
    mn = min(C)
    mx = max(C)
    if mx - mn < 0 or 1 < mx - mn:
        print("No")
        return
    elif mx - mn == 0:
        if mx == N - 1:
            neccessary = N
        else:
            neccessary = mx * 2
    elif mx - mn == 1:
        single = C[mn]
        if mx <= single:
            print("No")
            return
        neccessary = single + (mx - single) * 2
    if neccessary <= N:
        print("Yes")
    else:
        print("No")


if __name__ == "__main__":
    main()

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

Solution (Python):
from collections import Counter
n = int(input())
a = [int(item) for item in input().split()]

cnt = Counter(a)
if len(cnt) == 1:
    color_num = max(cnt)
    if color_num == n-1 or color_num <= n // 2:
        print("Yes")
    else:
        print("No")
elif len(cnt) == 2:
    color_num = max(cnt)
    
    if color_num - 1 not in cnt:
        print("No") 
        exit()
    if cnt[color_num-1] <= color_num - 1 and cnt[color_num-1] >= 2*color_num - n:
        print("Yes")
    else:
        print("No")
else:
    print("No")

Solution (Python):
from collections import Counter
from sys import exit
def No():
    print('No')
    exit()
def Yes():
    print('Yes')
    exit()

N = int(input())
A = list(map(int, input().split()))
if max(A) - min(A) > 1:
    No()
if len(set(A)) == 1:
    if A == [N-1]*N:
        Yes()
    a = A[0]
    if a <= N//2:
        Yes()
    No()
mA = min(A)
S = max(A)
B = Counter(A)[mA]
if B + 1 <= S <= (N - B)//2 + B:
    Yes()
No()

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

y=0

x = max(a)

if max(a)-min(a) >1:
    print("No")

elif max(a) == min(a):
    if max(a)==n-1 or n>=2*max(a):
        print('Yes')
    else:
        print('No')
        
else:
    for i in range(n):
        if a[i] == x:
            y += 1

    if y >=2*(x-(n-y)) and x-n+y >0 and n-y>0:
        print('Yes')
    else:
        print('No')

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

ma = max(a)
for ai in a:
    if ai < ma - 1:
        print("No")
        exit()

flag = 0
for ai in a:
    if ai == ma - 1:
        flag = 1


if flag:
    count = 0
    for ai in a:
        if ai == ma - 1:
            count += 1
    if (n - count) == 1:
        print("No")
        exit()
    count_min = count + 1
    count_max = count + (n - count) // 2
    if (count_min <= ma) and (ma <= count_max):
        print("Yes")
        exit()
    else:
        print("No")
        exit()


if ma == n-1:
    print("Yes")
    exit()
if (1 <= ma) and (ma <= n // 2):
    print("Yes")
    exit()
print("No")

Solution (Python):
import sys
input = sys.stdin.readline
N=int(input())
A=[int(i) for i in input().split()]
A.sort()
K=A[0]
x=0
y=0
for i in range(N):
    if A[i]!=K and A[i]!=K+1:
        print('No')
        sys.exit()
    if A[i]==K:
        x+=1
    elif A[i]==K+1:
        y+=1

if x==N and x >=2*K:
    print('Yes')
    sys.exit()
if x==N and N==K+1:
    print('Yes')
    sys.exit()
if x<=K and y>=(K-x+1)*2:
    print('Yes')
    sys.exit()
print('No')

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

unum = min(a)
vnum = max(a)

u = a.count(unum)
v = a.count(vnum)

exist = False

if vnum-unum == 1:
  if u+1 <= vnum <= u+v//2:
    exist = True
    
elif vnum == unum:
  if vnum == n-1 or vnum <= n//2:
    exist = True
    
print("Yes" if exist else "No")

Solution (Python):

def solve():
  n = int(input())
  a = list(int(i) for i in input().split())
  ans = 'Yes'
  alone = 0
  group = 0
  ha = max(a)
  la = min(a)
  if ha - la >= 2: ans = 'No'
  
  elif ha - la == 1:
   for i in a:
     if i == ha-1 : alone += 1
     else: group += 1
   if alone < ha and 2*(ha-alone)<= group: pass
   else: ans = 'No'
  
  
  elif ha - la == 0:
    if a[0] == n-1 or 2*a[0] <= n:
      pass
    else: ans = 'No'
  return ans

if __name__ == '__main__':
  print(solve())

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**9)
n=int(input())
A=list(map(int,input().split()))
a,b=max(A),min(A)
if a-b>1:
    print('No')
elif a==b:
    print('Yes' if a<=n//2 or a==n-1 else 'No')
else:
    c=A.count(b)
    print('Yes' if 1<=a-c<=(n-c)//2 else 'No')

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

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
a = list(li())

exist = True

if max(a) - min(a) > 1:
    exist = False
    
elif max(a) >= n:
    exist = False
    
elif max(a) - min(a) == 1:
    if a.count(min(a)) < max(a) and 2*(max(a)-a.count(min(a))) <= a.count(max(a)):
        exist = True
    else:
        exist = False
        
elif max(a) == min(a):
    if 2*a[0] <= n or a[0]+1 == n:
        exist = True
    else:
        exist = False
        
print("Yes") if exist else print("No")

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

a = list(map(int,input().split()))
a_max = max(a)
a_min = min(a)
if a_max > a_min+1:
    print('No')
    exit()
k = a_max
if a_max == a_min:
    if k == n-1 or 2*k <= n:
        print('Yes')
    else:
        print('No')
else:
    uni = a.count(k-1)
    if k >= uni + 1 and k <= uni + (n-uni)//2:
        print('Yes')
    else:
        print('No')

Solution (Python):


import sys
from collections import Counter


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


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


def solve():
    N = int(input())
    A = inpl()

    max_a = max(A)
    min_a = min(A)
    num_cnt = Counter(A)

    if max_a - min_a > 1:
        return 'No'

    if len(num_cnt) == 1:
        if max_a == N - 1 or 2 * max_a <= N:
            return 'Yes'
        else:
            return 'No'

    not_alone = num_cnt[max_a]
    alone = num_cnt[min_a]
    if not_alone >= (max_a - alone) * 2 and alone < max_a:
        return 'Yes'

    return 'No'


print(solve())

Solution (Python):


import sys
from collections import Counter


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


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


def solve():
    N = int(input())
    A = inpl()

    max_a = max(A)
    min_a = min(A)
    num_cnt = Counter(A)
    min_cnt = num_cnt[min_a]
    max_cnt = num_cnt[max_a]
    

    if max_a - min_a >= 2:
        return 'No'
    elif len(num_cnt) == 1:
        if min_a == N - 1:
            return 'Yes'
        elif min_a * 2 <= N:
            return 'Yes'
        else:
            return 'No'
    elif A.count(1) >= 2:
        return 'No'
    elif min_cnt == 2 and max_cnt >= max_a:
        return 'Yes'
    elif min_cnt == min_a:
        return 'Yes'
    else:
        return 'No'


print(solve())

Solution (Python):
N=int(input())
A=[int(i) for i in input().split()]
c_min=min(A)
c_max=max(A)
if c_min==c_max:
  if N//2>=c_min or N-1==c_min:
    print("Yes")
  else:
    print("No")
elif c_max-c_min==1:
  if c_max>=A.count(c_min)+1 and A.count(c_max)>=2*(c_max-A.count(c_min)):
    print("Yes")
  else:
    print("No")
else:
  print("No")

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
ctr = Counter(A)

if len(ctr) >= 3:
    print('No')
    exit()
if len(ctr) == 1:
    k,v = list(ctr.items())[0]
    if k == 1:
        print('Yes')
    elif N <= k:
        print('No')
    elif N >= 2*k:
        print('Yes')
    else:
        print('Yes' if k==N-1 else 'No')

else:
    (k1,v1),(k2,v2) = list(sorted(ctr.items()))
    if k2-k1 != 1 or k2 >= N:
        print('No')
    elif v1 > k1:
        print('No')
    else:
        print('Yes' if v2//2 + v1 >= k2 else 'No')

Solution (Python):





"""
agc016 B
"""

from collections import Counter

n = int(input())
acnt = Counter(map(int, input().split()))

if len(acnt) > 2:
    print('No')
    exit()

if len(acnt) == 1:
    [(a, nn)] = acnt.most_common(1)
    if a in range(1, n//2+1) or a == n-1:
        print('Yes')
    else:
        print('No')
    exit()

if len(acnt) == 2:
    [(a1, n1), (a2, n2)] = sorted(acnt.most_common(2), key=lambda x: x[0])
    if a2-a1 != 1:
        print('No')
    else:
        if 1 <= a2 - n1 <= (n-n1)//2:
            print('Yes')
        else:
            print('No')

Solution (Python):
n=input()
a=map(int, raw_input().split())
if max(a)-min(a)>=2:
	print "No"
	exit()
if max(a)==min(a):
	print "Yes" if a[0]==n-1 or a[0]*2<=n else "No"
else:
	one_cnt=a.count(min(a))
	more_cnt=min(a)+1-one_cnt
	print "No" if one_cnt+more_cnt*2>n or more_cnt<=0 else "Yes"

Solution (Python):
N=int(input())
A=[int(i) for i in input().split()]
M=max(A)
m=min(A)
if M-m>=2:
    print("No")
elif M-m==1:
    x=0
    y=0
    for i in range(N):
        if A[i]==m:
            x+=1
        else:
            y+=1
    if x<M and 2*(M-x)<=y:
        print("Yes")
    else:
        print("No")
else:
    if 2*M<=N or M==N-1:
        print("Yes")
    else:
        print("No")