qyliang/CodeNanoFix huggingface dataset:
Buggy code:
N=int(input())
A=list(map(int,input().split()))
B=[0]*N
C=[0]*N

for i in range(N):
    B[i]=A[i]+i
    C[i]=i-A[i]

B.sort()
B.insert(0,-2000000000)
B.append(2000000000)
C.sort()
ans=0
for a in C:
    l1=0
    r1=N
    while r1-l1>1:
        m1=(l1+r1)//2
        if B[m1] < a:
            l1=m1
        else:
            r1=m1
    l2=0
    r2=N
    while r2-l2>1:
        m2=(l2+r2)//2
        if B[m2] <= a:
            l2=m2
        else:
            r2=m2
    if l2==0 and l1 == -1:
        pass
    else:
        ans += l2-l1
    print(l2,l1)

print(ans)

Fixed code:
N=int(input())
A=list(map(int,input().split()))
B=[0]*N
C=[0]*N

for i in range(N):
    B[i]=A[i]+i
    C[i]=i-A[i]

B.sort()
B.insert(0,-2000000000)
B.append(2000000000)
C.sort()
ans=0
for a in C:
    l1=0
    r1=N
    while r1-l1>1:
        m1=(l1+r1)//2
        if B[m1] < a:
            l1=m1
        else:
            r1=m1
    l2=0
    r2=N
    while r2-l2>1:
        m2=(l2+r2)//2
        if B[m2] <= a:
            l2=m2
        else:
            r2=m2
    ans += l2-l1

print(ans)
Buggy code:
N=int(input())
A=list(map(int,input().split()))
B=[0]*N
C=[0]*N

for i in range(N):
    B[i]=A[i]+i
    C[i]=i-A[i]

B.sort()
C.sort()
ans=0
for a in C:
    l1=-1
    r1=N-1
    while r1-l1>1:
        m1=(l1+r1)//2
        if B[m1] < a:
            l1=m1
        else:
            r1=m1
    l2=0
    r2=N
    while r2-l2>1:
        m2=(l2+r2)//2
        if B[m2] <= a:
            l2=m2
        else:
            r2=m2
    if l2==0 and l1 == -1:
        pass
    else:
        ans += l2-l1
    print(l2,l1)

print(ans)

Fixed code:
N=int(input())
A=list(map(int,input().split()))
B=[0]*N
C=[0]*N

for i in range(N):
    B[i]=A[i]+i
    C[i]=i-A[i]

B.sort()
B.insert(0,-2000000000)
B.append(2000000000)
C.sort()
ans=0
for a in C:
    l1=0
    r1=N
    while r1-l1>1:
        m1=(l1+r1)//2
        if B[m1] < a:
            l1=m1
        else:
            r1=m1
    l2=0
    r2=N
    while r2-l2>1:
        m2=(l2+r2)//2
        if B[m2] <= a:
            l2=m2
        else:
            r2=m2
    if l2==0 and l1 == -1:
        pass
    else:
        ans += l2-l1

print(ans)
Buggy code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7

N = i()
A = l()
L = []
L2 = [0]*N*2
for i in range(N):
    L.append(i+A[i]+1)
    if i-A[i]+1 >= 0:
        L2[i-A[i]+1] += 1
ans = 0
for l in L:
    ans += L2[l]
print(ans)

Fixed code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7

N = i()
A = l()
L = []
L2 = [0]*(10**6)
for i in range(N):
    if i+A[i]+1 < 10**6:
        L.append(i+A[i]+1)
    if i-A[i]+1 >= 0 and i-A[i]+1 < 10**6:
        L2[i-A[i]+1] += 1
ans = 0
for l in L:
    ans += L2[l]
print(ans)
Buggy code:
ef main():
    from collections import Counter
    n = int(input())
    As = list(map(int,input().split(" ")))
    
    cnts = Counter()
    dp = [0]*n
    for i in range(1,n):
        dp[i] =dp[i-1] + cnts.get(i-As[i],0)
        cnts[As[i]+i]+=1
    print(dp[n-1])
main()

Fixed code:
def main():
    from collections import Counter
    n = int(input())
    As = list(map(int,input().split(" ")))
    
    cnts = Counter()
    dp = [0]*n
    cnts[As[0]+1]+=1
    for i in range(1,n):
        dp[i] =dp[i-1] + cnts.get(i+1-As[i],0)
        cnts[As[i]+i+1]+=1
    print(dp[n-1])
main()
Buggy code:
N = int(input())
A = [int(x) for x in input().split()]
L = [0] * 2 * N
R = [0] * 2 * N
for i in range(N):
    l = i + A[i]
    L[l] += 1
    r = i - A[i]
    R[r] += 1
ans = 0 
for i in range(N):
    ans += L[i] * R[i]
print(ans)


Fixed code:
N = int(input())
A = [int(x) for x in input().split()]
L = [0] * 2 * N
R = [0] * 2 * N
for i in range(N):
    l = i + A[i]
    if l < N: L[l] += 1
    r = i - A[i]
    if r > 0: R[r] += 1
ans = 0 
for i in range(N):
    ans += L[i] * R[i]
print(ans)
Buggy code:
n=int(input())
a=list(map(int,input().split()))
plus={}
minus={}
for i in range(n):
  if(a[i]+(i+1)<=10**9):
    if a[i]+(i+1) in plus.keys():
      plus[a[i]+(i+1)]+=1
    else:
      plus[a[i]+(i+1)]=1
  if(a[i]>(i+1)):
    if a[i]-(i+1) in minus.keys():
      minus[a[i]-(i+1)]+=1
    else:
      minus[a[i]-(i+1)]=1
ans=0
for i in plus.keys():
  if i in minus.keys():
    ans+=plus[i]*minus[i]
print(ans)

Fixed code:
n=int(input())
a=list(map(int,input().split()))
plus={}
minus={}
for i in range(n):
  if(a[i]+(i+1)<=10**9):
    if a[i]+(i+1) in plus.keys():
      plus[a[i]+(i+1)]+=1
    else:
      plus[a[i]+(i+1)]=1
  if(a[i]<(i+1)):
    if (i+1)-a[i] in minus.keys():
      minus[(i+1)-a[i]]+=1
    else:
      minus[(i+1)-a[i]]=1
ans=0
for i in plus.keys():
  if i in minus.keys():
    ans+=plus[i]*minus[i]
print(ans)
Buggy code:
n, *A = map(int, open(0).read().split())
ans = 0
for i, a in enumerate(A, 1):
    x, y = i+a, i-a
    d[x] = d.get(x, 0)+1
    ans += d.get(y, 0)
print(ans)

Fixed code:
n, *A = map(int, open(0).read().split())
ans, d = 0, {}
for i, a in enumerate(A, 1):
    x, y = i+a, i-a
    d[x] = d.get(x, 0)+1
    ans += d.get(y, 0)
print(ans)
Buggy code:
from collections import Counter
n = int(inputs[0])
*A, = (map(int, inputs[1].split()))
B = Counter([a+i for i, a in enumerate(A, 1)])
C = Counter([i-a for i, a in enumerate(A, 1)])
ans = 0
for k, v in B.items():
    if k in C:
        ans += C[k]*v
print(ans)

Fixed code:
from collections import Counter
inputs = open(0).readlines()
n = int(inputs[0])
*A, = (map(int, inputs[1].split()))
B = Counter([a+i for i, a in enumerate(A, 1)])
C = Counter([i-a for i, a in enumerate(A, 1)])
ans = 0
for k, v in B.items():
    if k in C:
        ans += C[k]*v
print(ans)
Buggy code:
N=int(input())
A=list(map(int, input().split()))
D={}
E={}
for i in range(N):
  a=i+A[i]
  b=i+A[i]
  D.setdefault(a,0)
  E.setdefault(b,0)
  D[a]+=1
  E[b]+=1
ans=0
for i in D.keys():
  if i in E.keys():
    ans+=D[i]*E[i]
print(ans)

Fixed code:
N=int(input())
A=list(map(int, input().split()))
D={}
E={}
for i in range(N):
  a=i+A[i]
  b=i-A[i]
  D.setdefault(a,0)
  E.setdefault(b,0)
  D[a]+=1
  E[b]+=1
ans=0
for i in D:
  if i in E:
    ans+=D[i]*E[i]
print(ans)
Buggy code:
N = int(input())

A = list(map(int, input().split()))
S = [-A[i] for i in range(N)]

D = {}
for i in range(N):
    if A[i] in D:
        D[A[i]][0] += 1
    else:
        D[A[i]] = [1, 0]
    if S[i] in D:
        D[S[i]][1] += 1
    else:
        D[S[i]] = [0, 1]
cnt = 0
for i in D:
    cnt += D[i][0] * D[i][1]
print(cnt)

Fixed code:
N = int(input())

A = list(map(int, input().split()))
S = [-A[i] for i in range(N)]

for i in range(N):
    A[i] += i
    S[i] += i
D = {}
for i in range(N):
    if A[i] in D:
        D[A[i]][0] += 1
    else:
        D[A[i]] = [1, 0]
    if S[i] in D:
        D[S[i]][1] += 1
    else:
        D[S[i]] = [0, 1]
cnt = 0
for i in D:
    cnt += D[i][0] * D[i][1]
print(cnt)
Buggy code:
from bisect import bisect_left, bisect_right

N = int(input())

A = list(map(int, input().split()))
Ap = [i - A[i] for i in range(N)]
Ap.sort()

print(Ap)
ans = 0
for i in range(N):
	ans += bisect_right(Ap, i + A[i]) - bisect_left(Ap, i + A[i])

print(ans)

Fixed code:
from bisect import bisect_left, bisect_right

N = int(input())

A = list(map(int, input().split()))
Ap = [i - A[i] for i in range(N)]
Ap.sort()

ans = 0
for i in range(N):
	ans += bisect_right(Ap, i + A[i]) - bisect_left(Ap, i + A[i])

print(ans)
Buggy code:
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
count = 0
b = []
c = []

for i in range(n):
    b.append(a[i]+i)
    c.append(i-a[i])
t = Counter(b)
for s in c:
    count += t[s]
    print(t[s],s)
print(t,1)
print(count)

Fixed code:
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
count = 0
b = []
c = []

for i in range(n):
    b.append(a[i]+i)
    c.append(i-a[i])
t = Counter(c) 
for s in b:
    count += t[s]

print(count)
Buggy code:
import sys,math,collections,
input = sys.stdin.readline
N=int(input())
A=list(map(int,input().split()))
B = []
C = []
for i,a in enumerate(A):
    B.append(i+a)
    C.append(i-a)
cnt = 0
Bc = collections.Counter(B)
Cc = collections.Counter(C)

for key,val in Bc.items():
    cnt += val*Cc[key]
print(cnt)


Fixed code:
import sys,collections
input = sys.stdin.readline
N=int(input())
A=list(map(int,input().split()))
B = []
C = []
for i,a in enumerate(A):
    B.append(i+a)
    C.append(i-a)
cnt = 0
Bc = collections.Counter(B)
Cc = collections.Counter(C)

for key,val in Bc.items():
    cnt += val*Cc[key]
print(cnt)
Buggy code:
n = int(input())
a = input().split()
pl = []
sb = []
for i in range(len(a)):
	pl.append(i+int(a[i])+1)
	if i - int(a[i]) > 1:
		sb.append(i-int(a[i])+1)


if len(sb) == 0:
	print('0')
else:

# spl = sorted(pl)
	isb = [int(i) for i in sb]
	ipl = [int(i) for i in pl]
	ssb = sorted(isb)
	t = min(ssb)
	# tl = [0] * max(ipl)
	tl = [0] * 9999999999999
	for i in ssb:
		if int(i)-1 < len(tl):
			tl[int(i)-1] += 1
	# print(pl)
	# print(sb)
	# print(tl)
	cnt = 0
	for i in pl:
		if int(i)-1 < len(tl):
			cnt += tl[int(i)-1]

	print(cnt)
	



Fixed code:
n = int(input())
a = input().split()
pl = []
sb = []
for i in range(len(a)):
	pl.append(i+int(a[i])+1)
	if i - int(a[i]) > 0:
		sb.append(i-int(a[i])+1)


if len(sb) == 0:
	print('0')
else:

# spl = sorted(pl)
	isb = [int(i) for i in sb]
	ipl = [int(i) for i in pl]
	ssb = sorted(isb)
	t = min(ssb)
	# tl = [0] * max(ipl)
	tl = [0] * 2000000	
	for i in ssb:
		if int(i)-1 < len(tl):
			tl[int(i)-1] += 1
	# print(pl)
	# print(sb)
	# print(tl)
	cnt = 0
	for i in pl:
		if int(i)-1 < len(tl):
			cnt += tl[int(i)-1]

	print(cnt)
Buggy code:
n = int(input())
a = list(map(int,input().split()))
b = [0]*n
for i in range(n):
    b[i]= i-a[0]-a[i]
d = defaultdict(int)
for i in range(1,n):
    d[b[i]] += 1
ans = 0
x = 0
for i in range(n-1):
    ans+=d[x]
    d[b[i+1]]-=1
    x -= -1+(a[i]-a[i+1])
print(ans)

Fixed code:
from collections import defaultdict
n = int(input())
a = list(map(int,input().split()))
b = [0]*n
for i in range(n):
    b[i]= i-a[0]-a[i]
d = defaultdict(int)
for i in range(1,n):
    d[b[i]] += 1
ans = 0
x = 0
for i in range(n-1):
    ans+=d[x]
    d[b[i+1]]-=1
    x -= -1+(a[i]-a[i+1])
print(ans)
Buggy code:
from collections import Counter

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

d = dict(collections.Counter([i + A[i-1] for i in range(1, N+1)]))
ans = sum([d.get(i - A[i-1], 0) for i in range(1, N+1)])

print(ans)


Fixed code:
from collections import Counter

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

d = dict(Counter([i + A[i-1] for i in range(1, N+1)]))
ans = sum([d.get(i - A[i-1], 0) for i in range(1, N+1)])

print(ans)
Buggy code:
n = int(input())
a = list(map(int, input().split()))

mp = [0] * n
#right = [0] * n

ans = 0

for i in range(n):
    ans += mp[i-a[i]]
    if n > a[i]+i:
        mp[a[i]+i] += 1

print(ans)

Fixed code:
n = int(input())
a = list(map(int, input().split()))

mp = [0] * n

ans = 0

for i in range(n):
    if i-a[i] >= 0:
        ans += mp[i-a[i]]
    if n > a[i]+i:
        mp[a[i]+i] += 1

print(ans)
Buggy code:
from collections import Counter

def main():
    n = int(input())
    a = list(map(int, input().split()))
    cnt = 0
    l1 = []
    for ea in a:
        l1.append(cnt - ea)
        cnt += 1
    l1c = Counter(l1)
    r = 0
    for ea2 in a:
        r += l1c[ea2 + a[ea2]]
    print(int(r))

if __name__ == '__main__':
    main()


Fixed code:
from collections import Counter

def main():
    n = int(input())
    a = list(map(int, input().split()))
    cnt = 0
    l1 = []
    for ea in a:
        l1.append(cnt - ea)
        cnt += 1
    l1c = Counter(l1)
    r = 0
    for i2 in range(n):
        r += l1c[i2 + a[i2]]
    print(int(r))

if __name__ == '__main__':
    main()
Buggy code:
n = int(input())
from collections import defaultdict
a = list(map(int,input().split()))
ans = 0
dp = defaultdict(int)
for e,i in enumerate(a):
    dp[i+e]+=1
    if i - e >= 0 :ans += dp[i-e]
print(ans)


Fixed code:
n = int(input())
from collections import defaultdict
a = list(map(int,input().split()))
ans = 0
dp = defaultdict(int)
for e,i in enumerate(a,1):
    if  e - i>= 0 :ans += dp[e-i]
    dp[i+e]+=1
print(ans)
Buggy code:
n = int(input())
a = list(map(int,input().split()))
plus = dict()
minus = dict()
for i in range(n):
  if (a[i]+i+1) in plus:
    plus[a[i]+i+1] += 1
  else:
    plus[a[i]+i+1] = 1
  if (a[i]+i+1) in minus:
    minus[i+1-a[i]] += 1
  else:
    minus[i+1-a[i]] = 1
c = 0
for v in plus.keys():
  if v in minus:
    c += plus[v] * minus[v]
print(c)

Fixed code:
n = int(input())
a = list(map(int,input().split()))
plus = dict()
minus = dict()
for i in range(n):
  num = i+1
  p = a[i] + num
  m = num - a[i]
  if p in plus:
    plus[p] += 1
  else:
    plus[p] = 1
  if (m) in minus:
    minus[m] += 1
  else:
    minus[m] = 1
c = 0
for v in plus.keys():
  if v in minus:
    c += plus[v] * minus[v]
print(c)
Buggy code:
n = int(input())
a = list(map(int,input().split()))
box = [[0,0] for i in range(n)]

for i in range(n):
    num = i + 1
    if 0 < num - a[i] < n:
        box[a[i]-num][0] += 1
    if 0 < a[i]+num < n:
        box[a[i]-num][1] += 1
ans = 0
for i in range(n):
    ans += box[i][0] * box[i][1]
print(ans)



Fixed code:
n = int(input())
a = list(map(int,input().split()))
box = [[0,0] for i in range(n)]

for i in range(n):
    num = i + 1
    if 0 < num - a[i] < n:
        box[num - a[i]][0] += 1
    if 0 < a[i]+num < n:
        box[a[i]+num][1] += 1
ans = 0
for i in range(n):
    ans += box[i][0] * box[i][1]
print(ans)
Buggy code:
n = int(input())
a = list(map(int, input().split()))
count = 0
d={}
for i in range(n):
    if i - a[i] in d:
        count+=d[i - a[i]]
    if i + a[i] in d:
        d[i+a[i]] + 1
    else:
        d[i+a[i]] = 1

print(count)

Fixed code:
n = int(input())
a = list(map(int, input().split()))
count = 0
d={}
for i in range(n):
    if i - a[i] in d:
        count+=d[i - a[i]]
    if i + a[i] in d:
        d[i+a[i]] += 1
    else:
        d[i+a[i]] = 1

print(count)
Buggy code:
from collections import Counter

N = int(input())
nums = [0] + list(map(int, input().split()))

A = [i + n for n in enumerate(nums, start=1)]
B = [j - n for n in enumerate(nums, start=1)]
BC = Counter(B)
answer = 0
for a in A:
  answer += BC[a]
print(answer)

Fixed code:
from collections import Counter

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

A = [i + n for i, n in enumerate(nums, start=1)]
B = [j - n for j, n in enumerate(nums, start=1)]
BC = Counter(B)
answer = 0
for a in A:
  answer += BC[a]
print(answer)
Buggy code:
n = int(input())
a = [int(i) for i in input().split()]

j_Aj = [0]
i_Ai = [0] * (max(j_Aj)+1)

for j, aj in enumerate(a):
    j_Aj.append(j + 1 - aj)
ans = 0
for j, j_aj in enumerate(j_Aj):
    if j == 0:
        continue
    if j_aj >= 2:
        ans += i_Ai[j_aj]
    if j + a[j - 1]<=max(j_Aj):
        i_Ai[j + a[j - 1]] += 1
print(ans)

Fixed code:
n = int(input())
a = [int(i) for i in input().split()]

j_Aj = [0]

for j, aj in enumerate(a):
    j_Aj.append(j + 1 - aj)
maxj=max(j_Aj)
i_Ai = [0] * (maxj+1)
ans = 0
for j, j_aj in enumerate(j_Aj):
    if j == 0:
        continue
    if j_aj >= 2:
        ans += i_Ai[j_aj]
    if j + a[j - 1]<=maxj:
        i_Ai[j + a[j - 1]] += 1
print(ans)
Buggy code:
n=int(input())
A=list(map(int,input().split()))
L,R={},{}
for i,a in enumerate(A):
  l,r = i-a,i+a
  if l in L: L[l]=0
  if r in R: R[r]=0
  L[l]+=1
  R[r]+=1

count = 0
for x in L:
  if x not in R: continue
  count += L[x]*R[x]
print(count)

Fixed code:
n=int(input())
A=list(map(int,input().split()))
L,R={},{}
for i,a in enumerate(A):
  l,r = i-a,i+a
  if l not in L: L[l]=0
  if r not in R: R[r]=0
  L[l]+=1
  R[r]+=1

count = 0
for x in L:
  if x not in R: continue
  count += L[x]*R[x]
print(count)
Buggy code:
n = int(input())
ai = [int(i) for i in input().split()]

li = [0]*(10**6+1)

for i in range(1,n):
    if ai[i] >= 2*10**5:
        continue
    li[ai[i]-i+5*10**5] += 1

ans = 0
exit()

for i in range(1,n):
    #print(i)
    ans += li[-ai[i-1]-(i-1)+5*10**5]
    if i == n:
        #print(i,i,i)
        break
    if ai[i] < 2*10**5:
        li[ai[i]-i+5*10**5] -= 1
        
print(ans)

Fixed code:
n = int(input())
ai = [int(i) for i in input().split()]

li = [0]*(10**6+1)

for i in range(1,n):
    if ai[i] >= 2*10**5:
        continue
    li[ai[i]-i+5*10**5] += 1

ans = 0

for i in range(1,n):
    if ai[i-1] >= 2*10**5:
        continue
    #print(i)
    ans += li[-ai[i-1]-(i-1)+5*10**5]
    if i == n:
        #print(i,i,i)
        break
    if ai[i] < 2*10**5:
        li[ai[i]-i+5*10**5] -= 1
        
print(ans)
Buggy code:
n=int(input())
y=list(map(int,input().split()))
from collections import Counter
lhs=Counter([j+y[j] for j in range(n)])
rhs=Counter([j-y[j] for j in range(n)])
count=0
for i in lhs:
   if i in rhs:
      count+=lhs[i]+rhs[i]

print(count)

Fixed code:
n=int(input())
y=list(map(int,input().split()))
from collections import Counter
lhs=Counter([j+y[j] for j in range(n)])
rhs=Counter([j-y[j] for j in range(n)])
count=0
for i in lhs:
   if i in rhs:
      count+=lhs[i]*rhs[i]

print(count)
Buggy code:
from collections import Counter

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

q1=[]
q2=[]

for i in range(n):
  q1.append(i+a[i])
  q2.append(i-a[i])

c2=Counter(q2)

ans=0
for i in c1.keys():
  ans+=c2[i]

print(ans)

Fixed code:
from collections import Counter

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

q1=[]
q2=[]

for i in range(n):
  q1.append(i+a[i])
  q2.append(i-a[i])

c1=Counter(q1)
c2=Counter(q2)

ans=0
for i in c1.keys():
  num1=c1[i]
  num2=c2[i]
  ans+=num1*num2

print(ans)
Buggy code:
n = int(input())
a = list(map(int,input().split()))
p = []
q = []
d = collections.defaultdict(int)
e = collections.defaultdict(int)
for i in range(n):
    p.append(a[i]+i+1)
for i in range(n):
    q.append(-a[i]+i+1)
ans = 0
for i in range(n):
    d[p[i]] += 1
    e[q[i]] += 1
for i in d.keys():
    ans += d[i]*e[i]
print(ans)

Fixed code:
n = int(input())
a = list(map(int,input().split()))
import collections
p = []
q = []
d = collections.defaultdict(int)
e = collections.defaultdict(int)
for i in range(n):
    p.append(a[i]+i+1)
for i in range(n):
    q.append(-a[i]+i+1)
ans = 0
for i in range(n):
    d[p[i]] += 1
    e[q[i]] += 1
for i in d.keys():
    ans += d[i]*e[i]
print(ans)
Buggy code:
def main():
    N = int(input())
    heights = list(map(int, input().split()))

    """
    for j>i, j-i = Ai + Aj
    i + Ai = j -Aj
    """

    L = [0] * N
    R = [0] * N

    count = 0
    for i, height in enumerate(heights):
        left = i + height
        right = i - height
        
        if left < N:
            L[left] += 1

        if right > 0:
            R[right] += 1
            count += L[right] * R[right]    

    print(count)


if __name__ == "__main__":
    main()


Fixed code:
def main():
    N = int(input())
    heights = list(map(int, input().split()))

    """
    for j>i, j-i = Ai + Aj
    i + Ai = j -Aj
    """

    L = [0] * N
    R = [0] * N

    count = 0
    for i, height in enumerate(heights):
        right = i + height
        left = i - height

        if right < N:
            R[right] += 1

        if left > 0:
            L[left] += 1
            count += R[left]

    print(count)


if __name__ == "__main__":
    main()
Buggy code:
N = int(input())
A = list(map(int, input().split()))

check = {}
ans = 0

for i in range(N):
    x = i - A[i]
    y = A[i] + i
    ans += check.get(x, 0)
    check.setdefault(y, 0)
    check[y] += 1

print(ans)
print(check)

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

check = {}
ans = 0

for i in range(N):
    x = i - A[i]
    y = A[i] + i
    ans += check.get(x, 0)
    check.setdefault(y, 0)
    check[y] += 1

print(ans)
Buggy code:
from collections import defaultdict


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

memo = defaultdict(int)
ans = 0
for i, x in enumerate(a, 1):
    print(memo)
    ans += memo[i - x]
    memo[x + i] += 1

print(ans)


Fixed code:
from collections import defaultdict


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

memo = defaultdict(int)
ans = 0
for i, x in enumerate(a, 1):
    ans += memo[i - x]
    memo[x + i] += 1

print(ans)
Buggy code:
import collections

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

L = [i+j for i, j in enumerate(A)]
R = [i-j for i, j in enumerate(A)]

l = collections.Counter(L)
r = collections.Counter(L)

for i in l.keys():
    ans += l[i]*r[i]

print(ans)

Fixed code:
import collections

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

L = [i+j for i, j in enumerate(A)]
R = [i-j for i, j in enumerate(A)]

l = collections.Counter(L)
r = collections.Counter(R)

ans = 0
for i in l.keys():
    ans += l[i]*r[i]

print(ans)
Buggy code:
from bisect import bisect_left, bisect, insort

def main():
    n = int(input())
    a = list(map(int, input().split()))
    h_plus_i = [ai+i for i, ai in enumerate(a, 1)]
    h_minus_i = [ai-i for i, ai in enumerate(a, 1)]
    s_h_plus_i = list(sorted(h_plus_i))
    s_h_minus_i = list(sorted(h_minus_i))
    ans = 0
    for hpi, hmi in zip(h_plus_i, h_minus_i):
        i1 = bisect_left(s_h_minus_i, -hpi)
        i2 = bisect_left(s_h_plus_i, -hmi)
        if (i1 < n and s_h_minus_i[i1] == -hpi) or (i2 < n and s_h_plus_i[i2] == -hmi):
            ans += 1
    print(ans)

if __name__ == "__main__":
    main()

Fixed code:
from bisect import bisect_left, bisect, insort

def main():
    n = int(input())
    a = list(map(int, input().split()))
    h_plus_i = [ai+i for i, ai in enumerate(a, 1)]
    h_minus_i = [ai-i for i, ai in enumerate(a, 1)]
    s_h_plus_i = list(sorted(h_plus_i))
    s_h_minus_i = list(sorted(h_minus_i))
    ans = 0
    for hpi, hmi in zip(h_plus_i, h_minus_i):
        i1 = bisect_left(s_h_minus_i, -hpi)
        i2 = bisect_left(s_h_plus_i, -hmi)
        if (i1 < n and s_h_minus_i[i1] == -hpi):
            ans += bisect_left(s_h_minus_i, -hpi+1)-i1
        if (i2 < n and s_h_plus_i[i2] == -hmi):
            ans += bisect_left(s_h_plus_i, -hmi+1)-i2
            
    print(ans//2)

if __name__ == "__main__":
    main()
Buggy code:
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
L = [0]*n
R = [0]*n
cnt=0
for i in range(n):
    L[i] = i+a[i]
    R[i] = i-a[i]
z1=Counter(L)
z2=Counter(R)
print(z1)
print(z2)
cnt=0
for k1,v1 in z1.items():
    v2 = z2[k1]
    cnt += v1*v2
print(cnt)

Fixed code:
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
L = [0]*n
R = [0]*n
cnt=0
for i in range(n):
    L[i] = i+a[i]
    R[i] = i-a[i]
z1=Counter(L)
z2=Counter(R)
cnt=0
for k1,v1 in z1.items():
    v2 = z2[k1]
    cnt += v1*v2
print(cnt)
Buggy code:
N = int(input())

A = tuple( map(int, input().split()) )

dict_ = {}

ans = 0
for i in range(N):
  k = (i+1) - A[i]
  if k in dict_:
    ans += dict[k]
  ad = (i+1) + A[i]
  if ad in dict_:
    dict_[ad] = dict_[ad] + 1
  dict_[ad] = 1

print(ans)

Fixed code:
N = int(input())

A = tuple( map(int, input().split()) )

dict_ = {}

ans = 0
for i in range(N):
  k = (i+1) - A[i]
  if k in dict_:
    ans += dict_[k]
  ad = (i+1) + A[i]
  if ad in dict_:
    dict_[ad] = dict_[ad] + 1
  else:
    dict_[ad] = 1

print(ans)