Problem:
 Score: 500 points    Problem Statement  You are the top spy of AtCoder Kingdom. To prevent the stolen secret from being handed to AlDebaran Kingdom, you have sneaked into the party where the transaction happens.  There are N attendees in the party, and they are given attendee numbers from 1 through N . The height of Attendee i is A_i .  According to an examination beforehand, you know that a pair of attendees satisfying the condition below will make the transaction.   The absolute difference of their attendee numbers is equal to the sum of their heights.   There are \frac{N(N-1)}{2} ways to choose two from the N attendees and make a pair. Among them, how many satisfy the condition above?  P.S.: We cannot let you know the secret.      Constraints   All values in input are integers.  2 \leq N \leq 2 \times 10^5  1 \leq A_i \leq 10^9\ (1 \leq i \leq N)         Input  Input is given from Standard Input in the following format:  N  A_1  A_2  \dots  A_N       Output  Print the number of pairs satisfying the condition.        Sample Input 1 6
2 3 3 1 3 1      Sample Output 1 3   A_1 + A_4 = 3 , so the pair of Attendee 1 and 4 satisfy the condition.  A_2 + A_6 = 4 , so the pair of Attendee 2 and 6 satisfy the condition.  A_4 + A_6 = 2 , so the pair of Attendee 4 and 6 satisfy the condition.   No other pair satisfies the condition, so you should print 3 .       Sample Input 2 6
5 2 4 2 8 8      Sample Output 2 0  No pair satisfies the condition, so you should print 0 .       Sample Input 3 32
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5      Sample Output 3 22

Solution (Python):
import numpy as np
from collections import Counter as cc
N = int(input())
A = list(map(int,input().split()))

ans = 0
A_p = cc(A+np.arange(N))
A_n = cc(np.arange(N) - A)
for k in range(1,N+1):
    ans += A_p[k]*A_n[k]
print(ans)

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

minus = [0] * N
plus = [0] * N

for i in range(N):
    minus[i] = (i + 1) - A[i]
    plus[i] = (i + 1) + A[i]

count = Counter(minus)
most = count.most_common()
num = len(most)

countPP = Counter(plus)


ans = 0

for i in range(num):
    elm = most[i]
    fir = elm[0]
    sec = elm[1]
    

    aa = countPP[fir]
    
    ans += aa * sec

    

print(ans)

Solution (Python):
N = int(input())
A = [int(a) for a in input().split()]
X = {}
for i,a in enumerate(A):
  v = i + a
  if not v in X:
    X[v] = [i]
  else:
    X[v].append(i)


out = 0
for i,a in enumerate(A):
  v = i - a
  
  if not v in X:
    continue
  cand = X[v]
  
  lb = 0
  ub = len(cand)
  while lb < ub:
    
    m = (lb + ub)//2
    if cand[m] >= i:
      lb = m + 1
    else:
      ub = m
  
  out += len(cand) - lb
print(out)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
d = dict()
ans = 0

d[n-1-a[-1]] = 1

for i in range(n-2,-1,-1):
    if a[i] + i in d:
        ans += d[a[i]+i]
    
    if i - a[i] in d:
        d[i-a[i]] += 1
    else:
        d[i-a[i]] = 1

print(ans)

Solution (Python):
N=int(input())
a=list(map(int,input().split()))
b=[0 for i in range(N)]
for i in range(N):
    x=a[i]
    a[i]=i-x
    b[i]=i+x
from collections import Counter
a=dict(Counter(a))
b=dict(Counter(b))
ans=0
for key in a.keys():
    if key in b.keys():
        ans+=a[key]*b[key]
print(ans)

Solution (Python):
n=int(input())
A=list(map(int, input().split()))
d={}
c=0

for i,a in enumerate(A):
  if i+a not in d:
    d[i+a]=1
  else:
    d[i+a]+=1
  if i-a in d:
    c+=d[i-a]

print(c)

Solution (Python):
def main():
    N=int(input())
    L=list(map(int,input().split()))
    L_i,L_j=[],[]
    L_memo = [0]*N
    cnt=0
    for i, j in enumerate(L):
        if i+j<N:
            L_memo[i+j]+=1
        if i-j>0:
            cnt+=L_memo[i-j]        
    print(cnt)
main()

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

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

l = {}
r = {}
for i in range(1,n+1):
    if i-a[i-1] in l:
        l[i-a[i-1]]+=1
    else:
        l[i-a[i-1]]=1

    
    
    if i+a[i-1] in r:
        r[i+a[i-1]]+=1
    else:
        r[i+a[i-1]]=1
    

for i in l:
    if i in r:
        ans += l[i]*r[i]
    


print(ans)

Solution (Python):
from collections import Counter
n = int(input())
aa = list(map(int, input().split()))
ar = [a+i for i, a in enumerate(aa)]
arc = Counter(ar)
al = [i-a for i, a in enumerate(aa)]
alc = Counter(al)
ans = 0
for k, c in arc.items():
    ans += c * alc[k]
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b = [0] * 10**6
ans=0
for i in range(n):
    if a[i]<(2*(10**5)):
        if i-a[i]>=0:
            ans+=b[i-a[i]]
        b[a[i]+i]+=1
print(ans)

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

"""
A[i] + A[j] = j - i
A[i] + i = j - A[j]

a = A[i] + i
b = j - A[j]
"""

ans = 0
dic = {}

for i in range(N-1, -1, -1):
    wa = A[i] + i
    if wa in dic:
        ans += dic[wa]
    sa = i - A[i]
    if sa in dic:
        dic[sa] += 1
    else:
        dic[sa] = 1
print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
a_index = []
for i in range(n):
    a_index.append((i, a[i]))
dic1 = {}
dic2 = {}
for i in range(n):
    one = a_index[i][0] + a_index[i][1]
    two = a_index[i][0] - a_index[i][1]
    if one not in dic1:
        dic1[one] = 1
    else:
        dic1[one] += 1
    if two not in dic2:
        dic2[two] = 1
    else:
        dic2[two] += 1
ans = 0
for i in dic1.keys():
    if i in dic2:
        ans += dic2[i] * dic1[i]
for i in dic2.keys():
    if i in dic1:
        ans += dic1[i] * dic2[i]
        
print(ans // 2)

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

L = {}
R = {}

for i in range(n):
    if i + a[i] in L:
        L[i + a[i]] = L[i + a[i]] + 1
    else:
        L[i + a[i]] = 1

    if i - a[i] in R:
        R[i - a[i]] = R[i - a[i]] + 1
    else:
        R[i - a[i]] = 1


ans = 0
for key in L.keys():
    if key in R:
        
        ans += L[key] * R[key]
print(ans)

"""
ans = 0
for i in range(n):
    for j in range(i+1, n):
        if abs(i-j) == a[i]+a[j]:
            ans += 1
print(ans)
"""

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


D = [0]*n
for i, a in enumerate(A):
    if 1+i+a < n:
        D[i+a] += 1
ans = 0

for i, a in enumerate(A):
    if 1+i-a >= 0:
        ans += D[i-a]
print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ans = 0
"""
ai + i = j - aj
"""
cnt = [0] * (n + 1)
for i in range(n):
    if 0 <= a[i] + i < n+1:
        cnt[a[i] + i] += 1

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

print(ans)

Solution (Python):
from collections import Counter

N = int(input())
h = [int(x) for x in input().split(" ")]
 
L = [0] * N
R = [0] * N
cnt = 0
for i in range(0, N):
    L[i] = i + 1 + h[i]
    R[i] = i + 1- h[i]
LC = Counter(L)
RC = Counter(R)

cnt = 0
for key,values in LC.most_common():
    if key in RC:
        num = RC[key]
        cnt += values * num

print(cnt)

Solution (Python):
from collections import Counter

N = int(input())
h = [int(x) for x in input().split(" ")]
 
L = [0] * N
R = [0] * N
cnt = 0
for i in range(0, N):
    L[i] = i + 1 + h[i]
    R[i] = i + 1- h[i]
LC = Counter(L)
RC = Counter(R)

cnt = 0
for key,values in LC.most_common():
    if key in RC:
        num = RC[key]
        cnt += values * num

print(cnt)

Solution (Python):

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

sa = [i-a[i] for i in range(n)]
wa = [i+a[i] for i in range(n)]

from collections import Counter

c_sa = Counter(sa)
c_wa = Counter(wa)
cand = set(c_sa.keys()) & set(c_wa.keys())
print( sum(c_sa[c]*c_wa[c] for c in cand) )

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

li1 = [i+1-A[i] for i in range(N)]
li2 = [A[i]+i+1 for i in range(N)]

cnt = [0 for i in range(2*N+1)]
for i in li2:
    if i<len(cnt)-1:
        cnt[i] += 1

ans = 0
for i in li1:
    if i>=0:
        ans += cnt[i]
print(ans)

Solution (Python):
from collections import Counter
 
N = int(input())
A = list(map(int, input().split()))
 
C_A1 = Counter([i + A[i] for i in range(N)])
C_A2 = Counter([i - A[i] for i in range(N)])

count = sum([C_A1[i]*C_A2[i] for i in range(N)])

print(count)

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

A1 = []
A2 = []

for i in range(N):
    A1.append(i + A[i])
    A2.append(i - A[i])

C_A1 = collections.Counter(A1)
C_A2 = collections.Counter(A2)
ans = 0
for i in range(N):
    ans += C_A1[i]*C_A2[i]
    
print (ans)

Solution (Python):
n = int(input())
A = [int(x) for x in input().split()]
res = 0
counter = {}
for i in reversed(range(n)):
    x = A[i]
    res += counter.get(x + i, 0)
    counter[i - x] = counter.get(i - x, 0) + 1
print(res)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
d = {}
for i in range(N):
    d[i] = 0
for i in range(N):
    j =  i - A[i] + 1
    if j >= 0:
        d[j] += 1
ans = 0
for i in range(N):
    b = A[i] + i + 1
    c = i - A[i] + 1
    if c >= 0:
        d[c] -= 1
    if 0 <= b <= N - 1:
        ans += d[b]
print(ans)

Solution (Python):

from collections import defaultdict

n = int(input())
a = list(map(int, input().split()))
lInfo = defaultdict(int)
rInfo = defaultdict(int)
cnt = 0

for i in range(n):
    lInfo[a[i]+i] += 1
    rInfo[i-a[i]] += 1

for k in lInfo.keys():
    cnt += lInfo[k] * rInfo[k]
print(cnt)

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


D = [0]*n
for i, a in enumerate(A):
    if i+a < n:
        D[i+a] += 1
ans = 0

for i, a in enumerate(A):
    if i-a >= 0:
        ans += D[i-a]
print(ans)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
D1 = {}
D2 = {}
def count(D, s):
    if s in D:
        D[s] += 1
    else:
        D[s] = 1
for i, a in enumerate(A):
        count(D1, a+i)
        count(D2, i-a)
ans = 0
for d, num in D1.items():
    if d in D2:
        ans += num * D2[d]
print(ans)

Solution (Python):
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
ci = defaultdict(int)
cj = defaultdict(int)
for i in range(n):
    ci[a[i]+i] += 1
    cj[i-a[i]] += 1
cnt = 0
for k in ci.keys():
    cnt += ci[k] * cj[k]
print(cnt)

Solution (Python):






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

cnt=0
"""
for i in range(n-1):
    for j in range(i+1,n-1):
        if j-i==a[j]+a[i]:
            
            cnt+=1

"""

R=dict()
L=dict()

for i in range(n):
    if a[i]+i+1 in R:
        R[a[i]+i+1]+=1
    else:
        R[a[i]+i+1]=1

    if i+1-a[i] in L:
        L[i+1-a[i] ]+=1
    else:
        L[i+1-a[i] ]=1


for i in R:
    if i in L:
        cnt+=R[i]*L[i]

print(cnt)

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

L = {}
R = {}
for i, a in enumerate(A):
    x = i + 1 + a
    L[x] = L[x] + 1 if x in L else 1

    x = i + 1 - a
    R[x] = R[x] + 1 if x in R else 1



ans = 0
for x in L.keys():
    ans += L[x] * (R[x] if x in R else 0)

print(ans)

Solution (Python):
import collections

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

L, R = [], []

for i in range(n):
    L.append(i+1+A[i])
    R.append(i+1-A[i])

cl = collections.Counter(L)
cr = collections.Counter(R)

ans = 0

for c in cl:
    ans += cl[c] * cr[c]

print(ans)

Solution (Python):
import bisect
inf = 10**15
mod = 10**9+7
n = int(input())
a = list(map(int, input().split()))
dicL = [a[i]+i for i in range(n)]
dicR = [i-a[i] for i in range(n)]
dicL.sort()
dicR.sort()
ans = 0
for x in range(n-1):
    indL = bisect.bisect_left(dicL, x)
    indR = bisect.bisect_left(dicR, x)
    countL = 0
    countR = 0
    while indL != n and dicL[indL] == x:
        countL+=1
        indL+=1
    while indR != n and dicR[indR] == x:
        countR+=1
        indR+=1
    ans+=countL*countR
print(ans)

Solution (Python):
n = int(input().strip())
a = list(map(int, input().split()))
dic = {}
ans = 0
for i in range(len(a)):
    if dic.get(i - a[i]):
        ans += dic[i - a[i]]
    if dic.get(a[i] + i):
        dic[a[i] + i] += 1
    else:
        dic[a[i] + i] = 1
print(ans)

Solution (Python):
import collections

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

l1 = []
l2 = []

for i in range(N):
  l1.append(i+1+A[i])
  l2.append(i+1-A[i])

c1 = collections.Counter(l1)
c2 = collections.Counter(l2)

sum = 0
for i in c1.keys():
  sum += c1[i]*c2[i]
 
print(sum)

Solution (Python):
from collections import defaultdict
n = int(input())
a = [int(i) for i in input().split()]
l_i = [a[i] + i + 1 for i in range(len(a))]
r_j = [i - a[i] + 1 for i in range(len(a))]
dl = defaultdict(int)
dr = defaultdict(int)
for i in range(n):
    dl[l_i[i]] += 1
    dr[r_j[i]] += 1

ans = 0
for i in dl.keys():
    ans += dl[i] * dr[i]

print(ans)

Solution (Python):
import collections

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

L = [i + A[i] for i in range(N)]
R = [i - A[i] for i in range(N)]

countL = collections.Counter(L)
countR = collections.Counter(R)

print(sum([countL[n] * countR[n] for n in countL.keys()]))

Solution (Python):
from collections import Counter

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

List1 = [i + A[i] for i in range(N)]
List2 = [i - A[i] for i in range(N)]

ans = 0

CList1 = Counter(List1)
CList2 = Counter(List2)

for i in range(N):
    ans += CList1[i]*CList2[i]
    
print (ans)

Solution (Python):
import collections

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

L = [i + A[i] for i in range(N)]
R = [i - A[i] for i in range(N)]

countL = collections.Counter(L)
countR = collections.Counter(R)

print(sum([countL[n] * countR[n] for n in countL.keys()]))

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

ListK = [i+1+List[i] for i in range(N)]
ListL = [i+1-List[i] for i in range(N)]

from collections import Counter 
ans = 0

NewlistK = Counter(ListK)
NewlistL = Counter(ListL)

for i in range(N):
  ans += (NewlistK[i])*(NewlistL[i])

print(ans)

Solution (Python):
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
d = defaultdict(int)
ans = 0
for i in range(n):
    ans += d[i-a[i]]
    d[i+a[i]] += 1
print(ans)

Solution (Python):
import collections

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

prev = collections.defaultdict(list)

ans = 0
for i, h in enumerate(A):
    
    

    comp = i - h

    if comp in prev:
        ans += len(prev[comp])
    prev[i + h].append(0)

print(ans)

Solution (Python):
def count_j(arg_ls, th, b_idx, e_idx):
	if b_idx-1 == e_idx: return 0
	if b_idx == e_idx: return int(arg_ls[b_idx] > th)
	cent_idx = (e_idx+1 - b_idx) // 2 + b_idx
	cent_j = arg_ls[cent_idx]
	if cent_j > th:
		return count_j(arg_ls, th, b_idx, cent_idx-1) + (e_idx+1 - cent_idx)
	else:
		return count_j(arg_ls, th, cent_idx, e_idx)

n = int(input())
inp_ls = input().split(" ")
a_ls = [int(e) for e in inp_ls[:n]]

i_dic = {}
for i,a in enumerate(a_ls):
	val = -a+(i+1)
	if val not in i_dic: i_dic[val] = []
	i_dic[val].append(i+1)

cnt = 0
for i in range(len(a_ls)):
	val = a_ls[i] + (i+1)
	cand_ls = i_dic.get(val, [])
	result = count_j(cand_ls, (i+1), 0, len(cand_ls)-1)
	cnt += result

print(cnt)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
from collections import defaultdict
n = int(input())
A = list(map(int, input().split()))
cnt = defaultdict(int)
ans = 0
for i in range(n):
    k = i-A[i]
    ans += cnt[k]
    cnt[i+A[i]]+=1
print(ans)

Solution (Python):
from collections import defaultdict
from itertools import product
import sys
import math
import bisect

sys.setrecursionlimit(200000)
input = sys.stdin.readline


def read():
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return N, A


def solve(N, A):
    
    X = [i-A[i] for i in range(N)]
    
    Y = [i+A[i] for i in range(N)]
    z = defaultdict(int)
    for y in Y:
        z[y] += 1
    return sum([z[x] for x in X])


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    print("{}".format(outputs))

Solution (Python):
from collections import Counter

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

P = []
Q = []

for i in range(N):
    P.append(i + 1 + A[i])
    Q.append(i + 1 - A[i])

P = Counter(P)
Q = Counter(Q)

res = 0

for v, c in P.items():
    if v in Q:
        res += c * Q[v]

print(res)

Solution (Python):
from collections import Counter
n = int(input())
p = Counter()
q = Counter()
for i, x in enumerate(input().split()):
    p[i-int(x)] += 1
    q[i+int(x)] += 1
print(sum(p[x]*q[x] for x in p.keys()))

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
import math
import copy

n = int(input())



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










kotae = 0
temp = 0
a = {}
b = {}

for i in range(n):
    temp = i - num[i]
    if (temp in a):
        a[temp] += 1
    else:
        a[temp] = 1

for i in range(n):
    temp = i + num[i]
    if (temp in b):
        b[temp] += 1
    else:
        b[temp] = 1

for i in range(200000):
    if ((i in a) and (i in b)):
        kotae += a[i] * b[i]

print(kotae)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
import collections

N = int(input())
A = list(map(int, input().split()))
B = {}
C = {}

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


ans = 0
for i in B:
    
    if i in C:
        ans += B[i]*C[i]
print(ans)

Solution (Python):
import collections

N = int(input())
A = list(map(int, input().split()))
B = collections.defaultdict(int)
C = collections.defaultdict(int)

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


ans = 0
for i in B:
    
    ans += B[i]*C[i]
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n = I()
    a = LI()
    c = collections.defaultdict(int)
    r = 0
    for i,t in enumerate(a):
        r += c[t-i]
        c[-t - i] += 1

    return r


print(main())

Solution (Python):
import collections

N = int(input())
A = list(map(int, input().split()))
B = collections.defaultdict(int)
C = collections.defaultdict(int)

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


ans = 0
for i in B:
    
    if i in C:
        ans += B[i]*C[i]
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
import math
from collections import defaultdict

def main():
    N = int(input())
    Al = [int(x) for x in input().split()]
    center = int(N/2)
    cnt = 0
    m = defaultdict(int)
    for i in range(N):
        m[i+Al[i]] += 1
        cnt += m[i-Al[i]]
    print(cnt)

if __name__ == '__main__':
    main()

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

ans = 0
B = [0]*N
C = [0]*N

for i in range(N):
  b = i+A[i]
  c = i-A[i]
  if 0 < b < N:
    B[b] += 1
  if 0 < c < N:
    C[c] += 1

for i in range(N):
  ans += B[i]*C[i]

print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
L = [0]*N
R = [0]*N
for i in range(N):
    l = i - A[i]
    r = i + A[i]
    if 0<l<N:
        L[l] += 1
    if 0<r<N:
        R[r] += 1


c = 0
for i in range(N):
    c += L[i]*R[i]
print(c)

Solution (Python):
from collections import Counter

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

m = Counter(i - elem for i, elem in enumerate(A))
res = 0
for i, elem in enumerate(A):
    res += m[i + elem]

print(res)

Solution (Python):












            

def count(dic,x):
    if x in dic:
        dic[x]+=1
    else:
        dic[x]=1
 
N=int(input())
A=list(map(int,input().split()))
dic1={}
dic2={}
s=0
for i in range(N):
    count(dic1,i-A[i])
    count(dic2,i+A[i])
for x in dic1:
    if x in dic2:
        s+=dic1[x]*dic2[x]
print(s)

Solution (Python):


from collections import Counter


def main():
    n = int(input())
    a = list(map(int, input().split()))
    m = Counter([i - elem for i, elem in enumerate(a)])
    ans = 0
    for i, elem in enumerate(a):
        ans += m[i + elem]
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
N=int(input())
A=[int(a) for a in input().split()]
X=[A[i-1]+i for i in range(1,N+1)]
Y=[-A[j-1]+j for j in range(1,N+1)]
cnt={}
for x in X:
    cnt[x]=[0,0]
for y in Y:
    cnt[y]=[0,0]
for x in X:
    cnt[x][0]+=1
for y in Y:
    cnt[y][1]+=1
ans=0
for key in cnt:
    p,q=cnt[key]
    ans+=p*q
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):


N = int(input())
A = list(map(int,input().split()))
L = [0] * (N+1)
R = [0] * (N+1)
for i in range(N):
    xl = i+1+A[i]
    xr = i+1-A[i]
    if 1 <= xl <= N-1:
        L[xl] += 1
    if 1 <= xr <= N-1:
        R[xr] += 1

ans = 0
for i in range(1,N+1):
    ans += L[i] * R[i]

print(ans)

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

index = list(range(1,N+1))

R = [index[i] - A[i] for i in range(N)]
L = [index[j] + A[j] for j in range(N)]

R_dict = collections.Counter(R)
L_dict = collections.Counter(L)

ans=0
for i in R_dict.keys()&L_dict.keys():
    ans+=R_dict[i]*L_dict[i]

print(ans)

Solution (Python):
def count(dic,x):
    if x in dic:
        dic[x]+=1
    else:
        dic[x]=1

N=int(input())
A=list(map(int,input().split()))
dic1={}
dic2={}
s=0
for i in range(N):
    count(dic1,i-A[i])
    count(dic2,i+A[i])
for x in dic1:
    if x in dic2:
        s+=dic1[x]*dic2[x]
print(s)

Solution (Python):
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
x = {}
ans = 0
for i in range(n):
    R = i + a[i]
    if not R in x:
        x[R]=1
    else: x[R]+=1
    
for i in range(n):
    L = i - a[i]
    if L in x:
        ans+=x[L]
print(ans)

Solution (Python):
def main():
    n=int(input())
    aa=list(map(int,input().split()))
    ll={}
    c=0
    for i,a in enumerate(aa,1):
        x=i-a
        if x in ll:
            c+=len(ll[x])
        y=i+a
        if y in ll:
            ll[y].append(i)
        else:
            ll[y]=[i]
    print(c)
main()

Solution (Python):
from collections import Counter
n = int(input())
li = list(map(int,input().split()))
 
r = [i-a for i, a in enumerate(li)]
l = [a+i for i, a in enumerate(li)]



c1 = dict(Counter(r))
c2 = dict(Counter(l))



cnt = 0
for k in c2.keys():
    if k in c1:
        cnt += c2[k] * c1[k]

print(cnt)

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

B1 = [i-a for i, a in enumerate(A)]
B2 = [a+i for i, a in enumerate(A)]

c1 = dict(Counter(B1))
c2 = dict(Counter(B2))

SUM = 0
for k in c2.keys():
    if k in c1:
        SUM += c2[k] * c1[k]

print(SUM)

Solution (Python):
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

from collections import defaultdict
n=n0()
a=[0]
a.extend(n1())

m1=[i+a[i] for i in range(n+1)]
m2=[j-a[j] for j in range(n+1)]

d1=defaultdict(list)
d2=defaultdict(list)
for i in range(1,n+1):
    d1[m1[i]].append(i)
    d2[m2[i]].append(i)

ans=0
for i in d1:
    ans+=len(d1[i])*len(d2[i])
            
print(ans)

Solution (Python):
import numpy as np
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

N = int(input())
A = np.array(list(map(int, input().split())))
num = np.arange(1,N+1)
P = num - A
Q = num + A
P.sort()
Q.sort()

ind_left = np.searchsorted(P, Q, side='left')
ind_right = np.searchsorted(P, Q, side='right')
cnt = np.sum(ind_right - ind_left)
print(cnt)

Solution (Python):


from collections import deque,Counter













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


lisI = []
for i,num in enumerate(lisA):
    lisI.append(i+num)

cnt = Counter(lisI[1:])
ans = 0
for j,num in enumerate(lisA):
    if j == 0:
        continue
    elif j-num in cnt:
        ans += cnt[j-num]
    else:
        continue

print(ans)

Solution (Python):
import collections
N = int(input())
A = [int(i) for i in input().split()]
B = collections.Counter([A[i]+i+1 for i in range(N)])


ans = 0
for i in range(N):
    ans += B[i+1-A[i]]

print(ans)

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

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

    L = {} 
    R = {} 
    for i,Ai in enumerate(height_list) :
        l = i + Ai
        if (l not in L.keys()) :
            L[l] = 0
        L[l] += 1

        r = i - Ai
        if (r not in R.keys()) :
            R[r] = 0
        R[r] += 1

    count = 0
    for l_key in L.keys() :
        if (l_key in R.keys()) :
            count += L[l_key] * R[l_key]
    
    print(count)


if __name__ == '__main__':
    main()

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

c1 = {}
c2 = {}

for i in range(N):
    c1.setdefault(i + A[i], 0)
    c1[i + A[i]] += 1
    c2.setdefault(i - A[i], 0)
    c2[i - A[i]] += 1

result = 0
for k in set(c1).intersection(c2):
    result += c1[k] * c2[k]
print(result)

Solution (Python):
from collections import Counter

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

L = Counter(l + a for l, a in enumerate(A))
R = Counter(r - a for r, a in enumerate(A))

print(sum(R[k] * v for k, v in L.items()))