qyliang/CodeNanoFix huggingface dataset:
Buggy code:
n = int(input())
a = [int(input()) for _ in range(n)]

c = 1
for i in range(n):
  if c == 2:
    print(i + 1)
    exit(0)
  c = a[c]
print(-1)

Fixed code:
n = int(input())
a = [int(input()) for _ in range(n)]

c = 1
for i in range(n):
  c = a[c - 1]
  if c == 2:
    print(i + 1)
    exit(0)

print(-1)
Buggy code:
n = int(input())
a = [int(input()) for _ in range(n)]

ans = 0

for i in range(n):
    if i == 0:
        now = a[0]-1
        ans += 1
    else:
        now = a[now]-1
        ans += 1
    if now == 1:
        print(ans)
        break
    if i == n:
        print(-1)



Fixed code:
n = int(input())
a = [int(input()) for _ in range(n)]

ans = 0

for i in range(n):
    if i == 0:
        now = a[0]-1
        ans += 1
    else:
        now = a[now]-1
        ans += 1
    if now == 1:
        print(ans)
        break
if i+1 == n:
    print(-1)
Buggy code:
N = int(input())
a_list = []
for n in range(N):
    a_list.append(int(input()))

count = 0
pos = 1
for n in range(N):
    pos = a_list[pos-1]
    count += 1
    if a_list[pos-1]==2:
        print(count)
        break
if a_list[pos]!=2:
    print(-1)

Fixed code:
N = int(input())
a_list = []
for n in range(N):
    a_list.append(int(input()))

count = 0
pos = 1
for n in range(N):
    count += 1
    if a_list[pos-1]==2:
        print(count)
        break
    pos = a_list[pos-1]
    
if a_list[pos-1]!=2:
    print(-1)
Buggy code:
N = int(input())

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

now=1-1
c=0

while c<N:
    now=A[now]-1
    c+=1
    if(now==2):
        print(now)
        break
else:
    print("-1")

Fixed code:
N = int(input())

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

now=1
c=0

while c<N:
    now=A[now-1]
    c+=1
    if(now==2):
        print(c)
        break
else:
    print("-1")
Buggy code:
N = int(input())
a = [int(input()) for _ in range(N)]

n = 0
count = 0
for i in range(N):
  count += 1
  n = a[n] - 1
  if n == 1:
    print(count)
    break  

print(count if n == 1 else -1)

Fixed code:
N = int(input())
a = [int(input()) for _ in range(N)]

n = 0
count = 0
for i in range(N - 1):
  count += 1
  n = a[n] - 1
  if n == 1:
    break 

print(count if n == 1 else -1)
Buggy code:
N=int(input())
a=[0]*N
for i in range(N):
  a[i]=int(input())

now=0
cnt=0
for j in range(N):
  #print(j,now,a[now]-1)
  if a[now]==2:
    print(cnt)
    break
  elif a[now]-1==now:
    print(-1)
    break
  else:
    now=a[now]-1
    cnt += 1
else:
  print(-1)

Fixed code:
N=int(input())
a=[0]*(N+1)
for i in range(1,N+1):
  a[i]=int(input())

now=1
cnt=0
for j in range(1,N+1):
  #print(j,cnt,now,a[now])
  if a[now]==2:
    cnt +=1
    print(cnt)
    break
  elif a[now]==now:
    print(-1)
    break
  else:
    now=a[now]
    cnt += 1
else:
  print(-1)
Buggy code:
import sys
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())

n = ni()

a = [ni() for _ in range(n)]
count = 0
nexti = 0
for i in range(n):
    if nexti == 1:
        print(count)
        exit()
    else:
        nexti = a[a[nexti] - 1]
        count += 1
print(-1)

Fixed code:
import sys
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())

n = ni()

a = [ni() for _ in range(n)]
count = 0
nexti = 0
for i in range(n):
    if nexti == 1:
        print(count)
        exit()
    else:
        nexti = a[nexti] - 1
        count += 1
print(-1)
Buggy code:
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
if not(2 in a):
    print(-1)

x = [False]*N
n,i = (1, 0)
while x[n] == False:
    x[n] = True
    n = a[n-1]
    i+=1
    if n==2:
        print(i)
        break
else:
    print(-1)

Fixed code:
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
x = [False]*N
n,i = (0, 0)
while x[n] == False:
    x[n] = True
    n = a[n]-1
    i+=1
    if n==1:
        print(i)
        break
else:
    print(-1)
Buggy code:
n = int(input())
a = [0] * n
for i in range(n):
    a.append(int(input()))
count = 0
num = 0

for i in range(n):
    num = a[num]
    count += 1
    if num == 2:
        break

if num != 2:
    count = -1

print(count)


Fixed code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
count = 0
num = 1

for i in range(n):
    num = a[num - 1]
    count += 1
    if num == 2:
        break

if num != 2:
    count = -1

print(count)
Buggy code:
n = int(input())
a = []
flg = False
start = 1
ans = 0
for _ in range(n):
    a.append(int(input()))
for i in range(10**5):
    if a[start - 1] == 2:
        ans +=1
        flg = True
        break
    else:
        ans +=1
        start = a[start] - 1
if flg:
    print(ans)
else:
    print(-1)

Fixed code:
n = int(input())
a = []
flg = False
start = 1
ans = 0
for _ in range(n):
    a.append(int(input()))
for i in range(n):
    if a[start - 1] == 2:
        ans +=1
        flg = True
        break
    else:
        ans +=1
        start = a[start - 1]
if flg:
    print(ans)
else:
    print(-1)
Buggy code:
import sys
n=int(input())
a=[]
f=[]
has2=False
for i in range(n):
  ai=int(input())
  if ai==2:
    has2=True
  a.append(ai)
  f.append(False)

if has2==False:
  print("-1")
  sys.exit()

f[0]=True
now=0
cnt=0
while True:
  if f[now]==True:
    print(-1)
    break
  if now==2:
    print(cnt)
    break
  f[now]=True
  now=a[now]
  cnt++

Fixed code:
import sys
n=int(input())
a=[]
f=[]
has2=False
for i in range(n):
  ai=int(input())-1
  if ai==1:
    has2=True
  a.append(ai)
  f.append(False)

if has2==False:
  print("-1")
  sys.exit()

now=0
cnt=0
while True:
  
  if f[now]==True:
    print(-1)
    sys.exit()
  if now==1:
    print(cnt)
    sys.exit()
  f[now]=True
  now=a[now]
  cnt+=1
Buggy code:
import sys
input = sys.stdin.readline

buttons = list(map(int,sys.stdin))
n = buttons[0]
count,light = 1,buttons[1]
while light!=2 and count<n:
    count,light = count+1,buttons[light]
print(count)

Fixed code:
import sys
input = sys.stdin.readline

buttons = list(map(int,sys.stdin))
n = buttons[0]
count,light = 1,buttons[1]
while light!=2 and count<n:
    count,light = count+1,buttons[light]
print(count if count<n else -1)
Buggy code:
n = int(input())
a = [int(input()) for _ in range(n)]

count = 1
i = 0

while count < n:
  i = a[i] - 1
  if i == 1:
    print(count)
    break
  count += 1
print(-1)


Fixed code:
n = int(input())
a = [int(input()) for _ in range(n)]

count = 1
i = 0

while count < n:
  i = a[i] - 1
  if i == 1:
    print(count)
    break
  count += 1
if count >= n:
  print(-1)
Buggy code:
n=int(input())
a=[(i,int(input())-1) for i in range(n)]
c,x=0,0
for i in range(n):
  x=a[x]
  if x==2:
    print(i+1)
    break
else:
  print(-1)

Fixed code:
n=int(input())
a=[int(input())-1 for i in range(n)]
c,x=1,0
for i in range(n):
  x=a[x]
  if x==1:
    print(i+1)
    break
else:
  print(-1)
Buggy code:
N = int(input())
a = [int(input()) for _ in range(N)]

button = 1
for i in range(N):
    button = a[button-1]
    if button == 2:
        print(i)
else:
    print(-1)

Fixed code:
N = int(input())
a = [int(input()) for _ in range(N)]

button = 1
for i in range(N):
    if button == 2:
        print(i)
        break
    button = a[button-1]
    
else:
    print(-1)
Buggy code:
n = int(input())
A = [int(input())-1 for _ in range(n)]

push = set([])
ans = 0
b = 0

while 1:
  if b in push:
    print(-1)
    exit()
  push.add(b)
  b = A[b]
  ans += 1

  if b== 2:
    print(ans)
    break
  

Fixed code:
n = int(input())
A = [int(input())-1 for _ in range(n)]

push = set([])
ans = 0
b = 0

while 1:
  if b in push:
    print(-1)
    exit()
  push.add(b)
  b = A[b]
  ans += 1

  if b== 1:
    print(ans)
    break
Buggy code:
x = input()
a = []
check = []
for i in range(int(x)):
    a.append(int(input()) - 1)
    check.append(1)

index = 0



while True:
    check[index] = 0
    index = a[index]
    if check[index] == 0:
        print(-1)
    if index == 1:
        print(count)


Fixed code:
x = input()
a = []
check = []
for i in range(int(x)):
    a.append(int(input()) - 1)
    check.append(1)

index = 0
count = 0


while True:
    count += 1
    check[index] = 0
    index = a[index]

    if check[index] == 0:
        print(-1)
        break
    if index == 1:
        print(count)
        break
Buggy code:
n = int(input())
a = [0]*n
for i in range(n):
    a[i] = int(input())

now = a[0]
cnt = 0

if cnt <= n:
    cnt += 1
    if now == 2:
        print(cnt)
        exit()
    now = a[now-1]

print(-1)

Fixed code:
n = int(input())
a = [0]*n
for i in range(n):
    a[i] = int(input())

now = a[0]
cnt = 0

while cnt <= n:
    cnt += 1
    if now == 2:
        print(cnt)
        exit()
    now = a[now-1]

print(-1)
Buggy code:
N = int(input())
A = [0]+[int(input()) for _ in range(N)]

v = {1}
x = 1
a = 0
while True:
  x = A[x]
  a += 1
  if x in v:
    print(-1)
    break
  if x == 2:
	print(a)
    break
  v.add(x)

Fixed code:
N = int(input())
A = [0]+[int(input()) for _ in range(N)]

v = set()
v.add(1)
x = 1
a = 0
while True:
  x = A[x]
  a += 1
  if x in v:
    print(-1)
    break
  if x == 2:
    print(a)
    break
  v.add(x)
Buggy code:
phai = 1
cnt = 0
loop_hantei = {1}

for _ in range(N):
    phai = A[phai-1]
    cnt += 1
    if phai in loop_hantei:
        print('-1')
        break
    else:
        loop_hantei.add(phai)
    if phai == 2:
        print(cnt)
        break

Fixed code:
N = int(input())
A = []
for _ in range(N):
    A.append(int(input()))

phai = 1
cnt = 0
loop_hantei = {1}

for _ in range(N):
    phai = A[phai-1]
    cnt += 1
    if phai in loop_hantei:
        print('-1')
        break
    else:
        loop_hantei.add(phai)
    if phai == 2:
        print(cnt)
        break
Buggy code:
n = int(input())
a = [0]
for _ in range(n+1):
  a.append(int(input()))
visited = [False]*(n+1)
visited[1] = True
cnt = 0
but = 1
while True:
  nextb = a[but]
  if visited[nextb]:
    cnt = -1
    break
  elif nextb == 2:
    cnt += 1
    break
  else:
    but = nextb
    cnt += 1
    visited[but] = True
print(cnt)
    

Fixed code:
n = int(input())
a = [0]
for _ in range(n):
  a.append(int(input()))
visited = [False]*(n+1)
visited[1] = True
cnt = 0
but = 1
while True:
  nextb = a[but]
  if visited[nextb]:
    cnt = -1
    break
  elif nextb == 2:
    cnt += 1
    break
  else:
    but = nextb
    cnt += 1
    visited[but] = True
print(cnt)
Buggy code:
n = int(input())
a = [int(input())-1 for _ in range(n)]
now = 0
checked = set()
for i in range(1, n+1):
  now = a[now]
  if now==1:
    print(i)
else:
  print(-1)

Fixed code:
n = int(input())
a = [int(input())-1 for _ in range(n)]
now = 0
checked = set()
for i in range(1, n+1):
  now = a[now]
  if now==1:
    print(i)
    break
else:
  print(-1)
Buggy code:
N = int(input())
b_list = []
for _ in range(N):
  b_list.append(int(input()))
if 2 not in b_list:
  print(-1)
else:
  i = b_list[0]
  keiyu_list = []
  cnt=0
  while b_list[i-1] not in keiyu_list:
    i = b_list[i-1]
    keiyu_list.append(i)
    cnt+=1
    if i == 2:
      cnt+1
      break
  if i==2:
    print(cnt)
  else:
    print(-1)

Fixed code:
N = int(input())
b_list = []
for _ in range(N):
  b_list.append(int(input())-1)

i = 0
cnt=0
keiyu_list = [0 for _ in range(N)]

while keiyu_list[i]==0:
  keiyu_list[i]=1
  i = b_list[i]
  cnt+=1
  if i == 1:
    cnt+1
    break
if i==1:
  print(cnt)
else:
  print(-1)
Buggy code:
def train(n):
    x = [int(input()) for i in range(n)]
    idx = x[0] - 1
    for i in range(n):
        if x[idx] == 2:
            return i + 1
        idx = x[idx] - 1
    return -1
n = int(input())
print(train(n))

Fixed code:
def train(n):
    x =[0] + [int(input()) for i in range(n)]
    idx = 1
    for i in range(n+1):
        if x[idx] == 2:
            return i + 1
        idx = x[idx]
    return -1
n = int(input())
print(train(n))
Buggy code:
n = int(input())
a = [int(input()) for i in range(n)]
now = a[0]
cnt = 1
for i in range(n):
    now = a[now - 1]
    if now == 2:
        cnt += 1
        print(cnt)
        exit()
print("-1")

Fixed code:
n = int(input())
a = [int(input()) for i in range(n)]
now = 0
cnt = 0
for i in range(n):
    #print(now)
    now = a[now] - 1
    cnt += 1
    if now == 1:
        
        print(cnt)
        
        exit()
print("-1")
Buggy code:
# -*- coding: utf-8 -*- #

n = int(input())
a_list = [int(input()) for i in range(n)]
ans = 0

if 1 in a_list:
    idx = a_list.index(1)
else:
    idx = 0

for i in range(n):
    if a_list[idx] == 2:
        break
    ans += 1
    idx = a_list[idx]-1

if ans == n:
    ans = -1

print(ans)

Fixed code:
# -*- coding: utf-8 -*- #

n = int(input())
a_list = [int(input()) for i in range(n)]
ans = 0
idx = 0

for i in range(n):
    ans += 1
    if a_list[idx] == 2:
        break
    idx = a_list[idx]-1

if ans == n:
    ans = -1

print(ans)
Buggy code:
N = int(input())
nums = [0]
for _ in range(N):
  nums.append(int(input()))
check_cnt = 1
_next = nums[1]
while True:
  if check_cnt >= N:
    break
  else:
    if next == 2:
      print(check_cnt)
      exit()
    _next = nums[_next]
    check_cnt += 1
print(-1)

Fixed code:
N = int(input())
nums = [0]
for _ in range(N):
  nums.append(int(input()))
check_cnt = 1
_next = nums[1]
while True:
  if check_cnt >= N:
    break
  else:
    if _next == 2:
      print(check_cnt)
      exit()
    _next = nums[_next]
    check_cnt += 1
print(-1)
Buggy code:
n = int(input())
a = [int(input()) for i in range(n)]
count = 0
i = 0
ans = False
while count < N:
    count += 1
    if a[i] == 2:
        ans = True
        break
    else:
        i = a[i] - 1
if ans == True:
    print(count)
else:
    print("-1")

Fixed code:
n = int(input())
a = [int(input()) for i in range(n)]
count = 0
i = 0
ans = False
while count < n:
    count += 1
    if a[i] == 2:
        ans = True
        break
    else:
        i = a[i] - 1
if ans == True:
    print(count)
else:
    print("-1")
Buggy code:
n = int(input())
a =[int(input()) for _ in range(n)]
b = a[0]
ans =1

for i in range(len(a)):
  if b==2:
    print(ans)
    exit()
  b = a[b-1]
  ans += 1
print


Fixed code:
n = int(input())
a =[int(input()) for _ in range(n)]
b = a[0]
ans =1

for i in range(len(a)):
  if b==2:
    print(ans)
    exit()
  b = a[b-1]
  ans += 1
print(-1)
Buggy code:
n = int(input())
l = [int(input()-1 for i in range(n))]

a = l[0]
for i in range(n):
    if a == 1:
        print(i+1)
        exit()
    a = l[a]
else:
    print(-1)

Fixed code:
n = int(input())
l = [int(input())-1 for i in range(n)]

a = l[0]
for i in range(n):
    if a == 1:
        print(i+1)
        exit()
    a = l[a]
else:
    print(-1)
Buggy code:
N = int(input())
list = [int(input()) for i in range(n)]
c = 0
x = 1
while x != 2 and c < N:
    x = list[x - 1]
    c += 1
if x == 2:
    print(c)
else:
    print(-1)


Fixed code:
N = int(input())
list = [int(input()) for i in range(N)]
c = 0
x = 1
while x != 2 and c < N:
    x = list[x - 1]
    c += 1
if x == 2:
    print(c)
else:
    print(-1)
Buggy code:
import sys

l = [int(i) for i in sys.stdin]

N = l[0]

a = l[1:]

done = [False] * N

i = 0

ans = 0

while True:
    if done[i] == False:
        done[i] = True
        ans += 1
        if a[i] == 1:
            break
        i = a[i]
    else:
        ans = -1
        break

print(ans)


Fixed code:
import sys

l = [int(i) for i in sys.stdin]

done = [False] * (l[0] + 1)

i = 1

ans = 0

while True:
    if done[i] == False:
        done[i] = True
        ans += 1
        if l[i] == 2:
            break
        i = l[i]
    else:
        ans = -1
        break

print(ans)
Buggy code:
n = int(input())
a = [int(input()) for i in range(n)]
check = [0 for i in range(n)]
now = 0
ans = 0
while check[now] == 0:
    if now == 1:
        print(ans)
        break
    check[now] = 1
    now = a[now-1]
    ans += 1
else:
    print(-1)

Fixed code:
n = int(input())
a = [int(input()) for i in range(n)]
check = [0 for i in range(n)]
now = 1
ans = 0
while check[now-1] == 0:
    if now == 2:
        print(ans)
        break
    check[now-1] = 1
    now = a[now-1]
    ans += 1
else:
    print(-1)
Buggy code:
n = int(input())
for i in range(n):
	a.append(int(input()))
t = [0]
ans = 1
if t == 2:
	print(ans)
else:
	while True:
		t = a[t-1]
		ans += 1
		if t == 2:
			print(ans)
			break
		if ans > n:
			print('-1')
			break

Fixed code:
n = int(input())
a = []
for i in range(n):
	a.append(int(input()))
t = a[0]
ans = 1
if t == 2:
	print(ans)
else:
	while True:
		t = a[t-1]
		ans += 1
		if t == 2:
			print(ans)
			break
		if ans > n:
			print('-1')
			break
Buggy code:
N = int(input())
a = [int(input()) for i in range(N)]
b = a[0]
count =1
for i in range(N-1):
	b = a[b-1]
	if b == 2:
		print(count)
		break
	count += 1
else:
	print(-1)

Fixed code:
N = int(input())
a = [int(input()) for i in range(N)]
b = a[0]
count = 0
for i in range(N-1):
	count += 1
	if b == 2:
		print(count)
		break
	b = a[b-1]
else:
	print(-1)
Buggy code:
n = int(input())
List = [int(input()) for _ in range(n)]
s = List[0]
count = 1
while s!=2 and count<n:
    count+=1
    s = List[s-1]
print(count if count>=n else -1)

Fixed code:
n = int(input())
List = [int(input()) for _ in range(n)]
s = List[0]
count = 1
while s!=2 and count<n:
    count+=1
    s = List[s-1]
print(-1 if count>=n else count)
Buggy code:
n = int(input())
al = [int(input()) for _ in range(n)]

pos = 0
for i in range(N):
    pos = A[pos] - 1
    if (pos == 1):
        print(i+1)
        exit()
print(-1)


Fixed code:
n = int(input())
al = [int(input()) for _ in range(n)]

pos = 0
for i in range(n):
    pos = al[pos] - 1
    if (pos == 1):
        print(i+1)
        exit()
print(-1)
Buggy code:
n = int(input())
a = [int(input())-1 for i in range(n)]
 
light = [False for i in range(n)]
cnt = 1
botan = a[0]
if botan == a[1]:
    pass
else:
    while cnt <= n:
        cnt += 1
        botan = a[botan]
        light[botan] = True
        if light[1]:
            break
 
if cnt < n:
    print(cnt)
else:
    print(-1)

Fixed code:
n = int(input())
a = [int(input())-1 for i in range(n)]
 
light = [False for i in range(n)]
cnt = 1
botan = a[0]
if botan == 1:
    pass
else:
    while cnt <= n:
        cnt += 1
        botan = a[botan]
        light[botan] = True
        if light[1]:
            break
 
if cnt < n:
    print(cnt)
else:
    print(-1)
Buggy code:
for i in range(n):
    a.append(int(input())-1)

button = 0
cnt = 0
done = [False] * n

while True:
    if done[button] == False:
        done[button] = True
        cnt += 1
        if a[button] == 1:
            break
        button = a[button]
    else:
        cnt = -1
        break

print(cnt)

Fixed code:
n = int(input())
a = []

for i in range(n):
    a.append(int(input())-1)

button = 0
cnt = 0
done = [False] * n

while True:
    if done[button] == False:
        done[button] = True
        cnt += 1
        if a[button] == 1:
            break
        button = a[button]
    else:
        cnt = -1
        break

print(cnt)
Buggy code:
n=int(input())
a=[int(input()) for _ in range(n)]

cnt=0
push=0
for i in range(n):
    push=a[push-1]
    cnt+=1
    if push == 1:
        print(cnt)
        exit()
print(-1)




Fixed code:
n=int(input())
a=[int(input()) for _ in range(n)]
cnt=0
push=1
for i in range(n):
    push=a[push-1]
    cnt+=1
    if push == 2:
        print(cnt)
        exit()
print(-1)
Buggy code:
import sys
n=int(input())
a=[int(input()) for _ in range(n)]
       
cnt=0
tmp=1

for i in range(n):
    cnt+=1
    already.append(tmp)
    tmp=a[tmp-1]
    if tmp==2:
        print(cnt)
        sys.exit()
print(-1)

Fixed code:
import sys
n=int(input())
a=[int(input()) for _ in range(n)]
       
cnt=0
tmp=1

for i in range(n):
    cnt+=1
    tmp=a[tmp-1]
    if tmp==2:
        print(cnt)
        sys.exit()
print(-1)
Buggy code:
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 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")

Fixed code:
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")
Buggy code:
import sys
input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

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()))
# rstrip().decode()



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 2*a-aa+2<=n:
			ans="Yes"




	print(ans)




















if __name__ == "__main__":
	main()


Fixed code:
import sys
input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

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()))
# rstrip().decode()



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()
Buggy code:
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:
        print("Yes")
        exit()
    else:
        print("No")
        exit()
ac=a.count(amin)
ad=n-ac
if (amax-ac)*2<=ad:
    print("Yes")
else:
    print("No")

Fixed code:
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")
Buggy code:
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()

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

Fixed code:
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()

# ma = mi + 1
if A.count(mi) <= mi and A.count(ma) >= (ma - A.count(mi)) * 2:
    print("Yes")
else:
    print("No")
Buggy code:
def answer(x):
  print(x)
  exit()

N=int(input())
A=list(map(int,input().split()))
if len(set(A))>=3:
  answer('Yes')
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-1)//2:
  answer('Yes')
else:
  answer('No')

Fixed code:
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')