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):
n = int(input())

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

memo = {}

ans = 0

for i in range(n):
    diff = i - a[i]
    memo.setdefault(diff, 0)
    ans += memo[diff]
    sum = a[i] + i
    memo.setdefault(sum, 0)
    memo[sum] += 1

print(ans)

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

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

memo = {}

ans = 0

for i in range(n):
    diff = i - a[i]
    if diff in memo:
         ans += memo[diff]
    sum = a[i] + i
    memo.setdefault(sum, 0)
    memo[sum] += 1

print(ans)

Solution (Python):
import collections
from collections import Counter
N=int(input())
a=input().split()
A=[int(number) for number in a]
pluss=[]
minuss=[]
number=1
for a in A:
    plus=a+number
    minus=number-a
    pluss.append(plus)
    minuss.append(minus)
    number+=1

m=Counter(minuss)

answer=0
for p in pluss:
    if p in m.keys():
        answer+=m[p]
print(answer)

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

Solution (Python):
from collections import defaultdict

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

cnt = 0
d = defaultdict(int)

for i in range(n):
    cnt += d[i-a[i]]
    d[i + a[i]] += 1

print(cnt)

Solution (Python):
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    B = [(i+1)+a for i, a in enumerate(A)]
    from collections import Counter
    c = Counter()

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

    print(ans)


if __name__ == '__main__':
    main()

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

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

n = int(input())
a = list(map(int, input().split()))
minus = Counter([i-a[i] for i in range(n)])
plus = Counter([i+a[i] for i in range(n)])

cnt = 0
for i in minus:
    if i in plus:
        cnt += minus[i] * plus[i]
print(cnt)

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

n: int = int(input())
a = list(map(int, input().split()))
minus = Counter([i-a[i] for i in range(n)])
plus = Counter([i+a[i] for i in range(n)])

cnt: int = 0
i: int
for i in minus:
    if i in plus:
        cnt += minus[i] * plus[i]
print(cnt)

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

n = int(input())
a = list(map(int, input().split()))
minus = Counter([i-a[i] for i in range(n)])
plus = Counter([i+a[i] for i in range(n)])

cnt = 0
for i in minus:
    if i in plus:
        cnt += minus[i] * plus[i]
print(cnt)

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


def rle(list):
    dict = {}
    for i in list:
        if i in dict.keys():
            dict[i] += 1
        else:
            dict[i] = 1
    return dict


n = int(input())
a = list(map(int, input().split()))
minus = Counter([i-a[i] for i in range(n)])
plus = Counter([i+a[i] for i in range(n)])

cnt = 0
for i in minus:
    if i in plus:
        cnt += minus[i] * plus[i]
print(cnt)

Solution (Python):
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)

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

dict_diffs = dict()
for i in range(1, n+1):
  dict_diffs[i+a[i-1]] = dict_diffs.get(i+a[i-1], 0) + 1
total = 0
for j in range(1, n+1):
  total += dict_diffs.get(j-a[j-1], 0)
print(total)

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

dict_diffs1 = dict()
dict_diffs2 = dict()
for i in range(1, n+1):
  dict_diffs1[i+a[i-1]] = dict_diffs1.get(i+a[i-1], 0) + 1
  dict_diffs2[i-a[i-1]] = dict_diffs2.get(i-a[i-1], 0) + 1
total = 0
for j in range(1, n+1):
  total += dict_diffs1.get(j-a[j-1], 0)
  total += dict_diffs2.get(j+a[j-1], 0)
total = total//2
print(total)

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

Solution (Python):
from collections import defaultdict

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

cnt = 0
d = defaultdict(int)

for i in range(n):
    cnt += d[i - A[i]]
    d[i + A[i]] += 1

print(cnt)

Solution (Python):

import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial
from bisect import bisect_left 
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep='\n')

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

d = defaultdict(int)
res = 0
for i, a in enu(A):
    v1 = i+a
    v2 = i-a
    res += d[v2]
    d[v1] += 1
print(res)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
ans = 0

dp = dict()
dm = dict()

for i,ai in enumerate(a):
    dp[i+ai] = dp.get(i+ai,0)+1
    dm[i-ai] = dm.get(i-ai,0)+1

for wa,i in dm.items():
    ans += i*dp.get(wa,0)

print(ans)

Solution (Python):
import collections

N = int(input())
hList = list(map(int, input().split()))
count = 0
n_p = collections.Counter([ i + hList[i] for i in range(len(hList))])

for i in range(N):
  if i - hList[i] > 0:
    count += n_p.get(i - hList[i], 0)

print(count)

Solution (Python):
def solve():
    N = int(input())
    As = [int(i) for i in input().split()]

    N_minus_A = [0] * N
    N_plus_A = [0] * N

    for i in range(N):
        minus = i + 1 - As[i]
        plus = i + 1 + As[i]
        if 0 < minus < N:
            N_minus_A[minus] += 1
        if 0 < plus < N:
            N_plus_A[plus] += 1
    print(sum([N_minus_A[i] * N_plus_A[i] for i in range(N)]))


if __name__ == '__main__':
    solve()

Solution (Python):
from collections import Counter

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

i_l=[]
j_l=[]
for i in range(n):
  i_l.append(i+a[i])
  j_l.append(i-a[i])

ci=Counter(i_l)
cl=Counter(j_l)

ans=0
for k,v in ci.items():
  ans+=v*cl[k]

print(ans)

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

add=[]
sub=[]
for i in range(n):
    add.append(i+a[i])
    sub.append(i-a[i])

add_dic={}
sub_dic={}

for l in add:
    if l in add_dic:
        add_dic[l]+=1
    else:
        add_dic[l]=1

for l in sub:
    if l in sub_dic:
        sub_dic[l]+=1
    else:
        sub_dic[l]=1

ans=0

for i,k in add_dic.items():
    if i in sub_dic:
        ans+=k*sub_dic[i]

print(ans)

Solution (Python):
N = int(input())
*A, = map(int, input().split())
dic = {}
ans = 0
for i in range(N):
    if A[i] + i in dic:
        dic[A[i] + i] += 1
    if A[i] + i not in dic:
        dic[A[i] + i] = 1
    if i - A[i] in dic:
        ans += dic[i - A[i]]
print(ans)

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

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

NA=[]

cnt=0
d={}
for i in range(N):
    if A[i]-i in d:
        cnt+=d[A[i]-i]
    if not -A[i]-i in d:
        d[-A[i]-i]=1
    else:
        d[-A[i]-i]+=1

print(cnt)

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

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

NA=[]

for i in range(N):
    NA.append(A[i]-i)

from collections import Counter

d=Counter(NA)

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

print(cnt)

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

P = [i + a for i, a in enumerate(A, start=1)]
Q = [j - a for j, a in enumerate(A, start=1)]

ans = 0
QC = Counter(Q)
for p in P:
    ans += QC[p]

print(ans)

Solution (Python):
from collections import defaultdict
n = int(input())
L_dict, R_dict = defaultdict(int), defaultdict(int)
i = 0
for ipt in input().split():
    L_dict[i + int(ipt)] += 1
    R_dict[i - int(ipt)] += 1
    i += 1
cnt = 0
for Lk, Lv in L_dict.items():
        cnt += R_dict[Lk] * Lv
print(cnt)

Solution (Python):
from collections import defaultdict
n = int(input())
L_dict, R_dict = defaultdict(int), defaultdict(int)
i = 0
for ipt in input().split():
    L_dict[i + int(ipt)] += 1
    R_dict[i - int(ipt)] += 1
    i += 1
cnt = 0
for Lk, Lv in L_dict.items():
    try:
        cnt += R_dict[Lk] * Lv
    except KeyError:
        pass
print(cnt)

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

P = [i + a for i, a in enumerate(A, start=1)]
Q = [j - a for j, a in enumerate(A, start=1)]

ans = 0
QC = Counter(Q)
for p in P:
    ans += QC[p]

print(ans)

Solution (Python):

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

L = dict(); R = dict()
for i in range(N):
    L[i + A[i]] = L.get(i + A[i], 0) + 1
    R[i - A[i]] = R.get(i - A[i], 0) + 1

ans = 0

for i in range(N):
    ans += L.get(i, 0) * R.get(i, 0)

print(ans)

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

L = dict(); R = dict()
for i in range(N):
    l = i + A[i]
    if l in L:
        L[l] += 1
    else:
        L[l] = 1
    r = i - A[i]
    if r in R:
        R[r] += 1
    else:
        R[r] = 1

ans = 0
for x in R:
    if x in L:
        ans += L[x] * R[x]

print(ans)

Solution (Python):
from collections import Counter
n = int(input())
a1, a2 = [], []
for idx, ai in enumerate(map(int, input().split())):
    a1.append(ai+idx)
    a2.append(idx-ai)
cnt = Counter(a2)
res = 0
for idx, i in enumerate(a1):
    cnt[a2[idx]] -= 1
    res += cnt[i]
print(res)

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

a=[]
b={}
ans=0

for i in range(n):
    a.append(i+l[i])
    b[i-l[i]] = b.get(i-l[i],0) + 1

for av in a:
    ans = ans + b.get(av,0)

print(ans)

Solution (Python):



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


"""
indexのi,jについて、 j - i = A[i] + A[j] になる組の数を数え上げる。
愚直にやると、i,jを全ペアためす。N(O^2)で間に合わない

二つの変数が出てきて、ある式で関係性が表せる場合は、iの式 = jの式 みたいにしてやるとうまくいきことが多い
j - i = A[i] + A[j] 
-> A[i] + i = j - A[j]

なので、各参加者について、iとして使う時とjとして使うときで分けて数えていって、最後に組の数を数え上げる
A[i], i >=1 なので、２以上
1 <= j <= N, A[j] >= 1 なので、N未満
の範囲を調べればよい
"""
from collections import defaultdict
I = defaultdict(int)
J = defaultdict(int)

for i in range(N):
    I[A[i] + i+1] += 1
    J[i+1 - A[i]] += 1

ans = 0
for i in range(2, N):
    ans += I[i] * J[i]

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split(' ')))
b = [a[i] + i for i in range(n)]
c = [j - a[j] for j in range(n)]
d = dict()
ans = 0
for i in range(n):
  if d.get(b[i]) is None:
    d.setdefault(b[i],1)
  else:
    d[b[i]] += 1
  if d.get(c[i]) is not None:
    ans += d[c[i]]
print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
P = {}
Q = {}
X = {}
for i in range(N):
  p = i+1+A[i]
  q = i+1-A[i]
  if str(p) in P:
    P[str(p)] += 1
  else:
    P[str(p)] = 1
  if str(q) in Q:
    Q[str(q)] += 1
  else:
    Q[str(q)] = 1
  X.setdefault(str(p),i+1+A[i])
  X.setdefault(str(q),i+1-A[i])



ans = 0

for key in X.keys():
  if (key in P) and (key in Q):
  	ans += P[key] * Q[key]

print(ans)

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

L={}
R={}
ans=0
for i in range(N):
    if i+A[i] in L:
        L[i+A[i]]+=1
    else:
        L[i+A[i]]=1
    if A[i]<=i:
        if i-A[i] in R:
            R[i-A[i]]+=1
        else:
            R[i-A[i]]=1
for r in R:
    if r in L:
        ans+=L[r]*R[r]
print(ans)

Solution (Python):



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

x = []
y = {}

for i, elem in enumerate(a):
    x.append(i+a[i])
    y[i-a[i]] = y.get(i-a[i], 0) + 1

ans = 0

for i in x:
    ans += y.get(i, 0)

print(ans)

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

x = []
y = {}

for i, elem in enumerate(a):
    x.append(i+a[i])
    y[i-a[i]] = y.get(i-a[i], 0) + 1

ans = 0

for i in x:
    ans += y.get(i, 0)

print(ans)

Solution (Python):



from collections import defaultdict

def main():
  n = int(input())
  la = list(map(int, input().split()))
  cum = defaultdict(int)
  for i, a in enumerate(la, 1):
    cum[i - a] += 1
    
  cnt = 0
  for i, a in enumerate(la, 1):
    cnt += cum[a + i]
  print(cnt)

if __name__=='__main__':
  main()

Solution (Python):
J = [0 for i in range(int(2e5+3))]


def main():
    n = int(input())
    a = list(map(lambda x: int(x)-1, input().split()))

    for i in range(n):
        tmp = i - a[i]
        if 0 < tmp:
            J[tmp] += 1

    cnt = 0
    for i in range(n):
        tmp = a[i]+i+2
        if tmp < len(J)-1:
            cnt += J[tmp]

    print(cnt)
    return


if __name__ == "__main__":
    main()

Solution (Python):

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-A[i-1]]
    memo[i+A[i-1]] += 1
print(ans)

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

Solution (Python):


def abc166_e():
    _ = int(input())
    A = list(map(int, input().split()))
    ans = 0
    from collections import defaultdict
    d = defaultdict(int)
    for i, x in enumerate(A, 1):
        ans += d[i - x]
        d[x + i] += 1
    print(ans)

abc166_e()

Solution (Python):
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)

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

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

ans = 0
mp = {}

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

print(ans)

Solution (Python):
import sys
from collections import Counter
N = int(input())

A = list(map(int,input().split()))
L = []
R = []
for i in range(N):
  L.append(A[i]+i+1)
  R.append(i+1 - A[i])
L.sort()
R.sort()
R = Counter(R)

cnt = 0
for i in range(N):
  cnt += R[L[i]]

print(cnt)

Solution (Python):
import bisect


def main():
    N = int(input())
    A = list(map(int, input().split(' ')))
    pos_scores = [i + 1 + a for i, a in enumerate(A)]
    neg_scores = [i + 1 - a for i, a in enumerate(A)]
    neg_scores.sort()
    cnt = 0
    for ps in pos_scores:
        left = bisect.bisect_left(neg_scores, ps)
        right = bisect.bisect_right(neg_scores, ps)
        cnt += right - left
    print(cnt)


if __name__ == '__main__':
    main()

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

add1 = []
sub1 = []
for i in range(n):
    add1.append(a[i] + i)
    sub1.append(i - a[i])

dict1 = {}
dict2 = {}
for i in add1:
    if i in dict1:
        dict1[i] += 1
    else:
        dict1[i] = 1
for i in sub1:
    if i in dict2:
        dict2[i] += 1
    else:
        dict2[i] = 1

ans = 0
for k,v in dict1.items():
    if k in dict2:
        ans += v * dict2[k]

print(ans)

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

tmp_i = [a_input[i]-i for i in range(n)]
tmp_j = [-a_input[i]-i for i in range(n)]

count_i = Counter(tmp_i)
count_j = Counter(tmp_j)

ans=0
for k,v in count_i.items():
    ans+=v*count_j.get(k,0)
print(ans)

Solution (Python):
import collections

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

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

print(ans)

Solution (Python):
import sys
import bisect
from collections import deque
import copy
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    plus_list = []
    minus_list = []
    for i in range(N):
        plus_list.append((i+1) + A[i])
        minus_list.append((i+1) - A[i])
    plus_list.sort()
    minus_list.sort()
    count = 0
    for i in range(N):
        count += bisect.bisect_right(minus_list, plus_list[i]) - bisect.bisect_left(minus_list, plus_list[i])
    print(count)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
input=lambda :sys.stdin.readline().rstrip()
from collections import defaultdict

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

d = defaultdict(int)
ans = 0
for j in range(n):
    Dif = j - A[j]
    ans += d[Dif]
    Sum = A[j] + j
    d[Sum] += 1
print(ans)

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

J=[a[i-1]+i for i in range(1,n+1)]
I=[i-a[i-1] for i in range(1,n+1)]
dj=Counter(J)
di=Counter(I)
ans=0
for i in dj:
  if i in di:
    ans+=(dj[i]*di[i])
print(ans)

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

C = {}
s = 0
for i in range(len(A), 0, -1):
    t = i - A[i-1]
    if not(t in C):
        C[t] = 0
    C[t] += 1

    k = i + A[i-1]
    if k in C:
        s += C[k]

print(s)

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

B = Counter(i + a for i, a in enumerate(A))
C = Counter(j - a for j, a in enumerate(A))


for k, v in B.items():
    cnt += C[k] * v
print(cnt)

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

l = Counter()
r = Counter()

for i, a in enumerate(A):
    i += 1
    l[i+a] += 1
    r[i-a] += 1

res = 0
for k, v in l.items():
    res += v*r[k]

print(res)

Solution (Python):
from collections import defaultdict

N = int(input())
A = [0] + list(map(int, input().split()))
d = defaultdict(int)

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

print(ans)

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
p_map = {}
q_map = {}
for i, A in enumerate(a):
    p = i + A
    q = i - A
    if p not in p_map:
        p_map[p] = 1
    else:
        p_map[p] += 1
    if q not in q_map:
        q_map[q] = 1
    else:
        q_map[q] += 1
ans = 0
for i, v in p_map.items():
    if i in q_map:
       ans += v*q_map[i]
print(ans)

Solution (Python):

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

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

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)

int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

def solve():
    n = II()
    A = LI()

    memo = {}
    ans = 0
    for j in range(n):
        sa = j - A[j]
        ans += memo.get(sa, 0)
        wa = j + A[j]
        memo[wa] = memo.get(wa, 0) + 1
        
    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
dic1,dic2,freq1,freq2=[0]*(n+1),[0]*(n+1),{},{}
for i,a in enumerate(a,1):
  dic1[i]=i+a
  dic2[i]=i-a

for i in range(n+1):
  if freq1.get(dic1[i],0)==0:
    freq1[dic1[i]]=1
  else:
    freq1[dic1[i]]+=1
    
for i in range(n+1):
  if freq2.get(dic2[i],0)==0:
    freq2[dic2[i]]=1
  else:
    freq2[dic2[i]]+=1

ans=0
for i in range(1,n+1):
    x=dic1[i]
    if freq2.get(x,0)!=0:
        ans+=freq2[x]
        
        
print(ans)

Solution (Python):
from collections import Counter
n = int(input())
lst = list(map(int, input().split()))
lst1 = []
lst2 = []
for i in range(n):
  lst1.append(i+lst[i])
  lst2.append(i-lst[i])
lst1 = Counter(lst1)
lst2 = Counter(lst2)
count = lst1.most_common()
ans = 0
for i in range(len(count)):
  a = lst1[count[i][0]]
  b = lst2[count[i][0]]
  ans += a*b
print(ans)

Solution (Python):
from collections import defaultdict

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

CNT = defaultdict(int)
ans = 0

for i, a in enumerate(A):
    ima = i-a
    if ima in CNT:
        ans += CNT[ima]

    CNT[i+a] += 1

print(ans)

Solution (Python):
n=int(input())
a=[int(j) for j in input().split()]
from collections import defaultdict
c=defaultdict(int)
for i in range(n):
    c[i+1-a[i]]+=1
ans=0
for i in range(n):
    ans+=c[a[i]+i+1]

print(ans)

Solution (Python):
import sys
import re
import math
import collections
import decimal
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics

sys.setrecursionlimit(10000001)
INF = sys.maxsize
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))



def main():
    n = ni()
    a = na()

    larger = [i - a[i] for i in range(n)]
    larger_cnt = collections.Counter(larger)

    ans = 0
    for j in range(n):
        smaller = a[j] + j
        larger_cnt[j - a[j]] -= 1
        if smaller in larger_cnt.keys():
            if larger_cnt[smaller] != 0:
                ans += larger_cnt[smaller]

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import re
import math
import collections
import decimal
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics

sys.setrecursionlimit(10000001)
INF = sys.maxsize
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))



def main():
    n = ni()
    a = na()

    larger = [(i + 1) - a[i] for i in range(n)]
    larger_cnt = collections.Counter(larger)

    ans = 0
    for j in range(n):
        smaller = a[j] + (j + 1)
        larger_cnt[(j + 1) - a[j]] -= 1
        if smaller in larger_cnt.keys():
            if larger_cnt[smaller] != 0:
                ans += larger_cnt[smaller]

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
l = {}
r = {}
xc = 0
for x in input().split():
    xi = int(x)
    lx = xc + xi
    if lx in l:
        l[lx] += 1
    else:
        l[lx] = 1
    rx = xc - xi
    if rx in r:
        r[rx] += 1
    else:
        r[rx] = 1
    xc += 1
c = 0
for x in l:
    if x in r:
        c += l[x] * r[x]
print(c)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
i_Ai = {}
cnt = 0

for i, h in enumerate(A):
	imh = i - h
	if imh in i_Ai:
		cnt += i_Ai[imh]
	iph = i + h
	if iph in i_Ai:
		i_Ai[iph] += 1
	else:
		i_Ai[iph] = 1
print(cnt)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
d_l = {}
d_r = {}
ans = 0
for i, x in enumerate(a):
    l = i + x
    r = i - x
    if i == 0:
        d_l[l] = 1
    elif i == n - 1:
        if r in d_r:
            d_r[r] += 1
        else:
            d_r[r] = 1
    else:
        if l in d_l:
            d_l[l] += 1
        else:
            d_l[l] = 1
        if r in d_r:
            d_r[r] += 1
        else:
            d_r[r] = 1
for i, j in d_l.items():
    if i in d_r.keys():
        ans += j * d_r[i]
print(ans)

Solution (Python):
from collections import defaultdict

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

a_l = defaultdict(int)
a_r = defaultdict(int)

for i in range(n):
    a_l[i+a[i]] += 1
    a_r[i-a[i]] += 1

cnt = 0
for k in set(a_l.keys()) & set(a_r.keys()):
    cnt += a_l[k] * a_r[k]

print(cnt)

Solution (Python):
from collections import Counter

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

B = Counter(i - A[i] for i in range(N))
C = Counter(i + A[i] for i in range(N))
ans = 0
for v, num in B.items():
    ans += C[v] * num
print(ans)

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
B = []
C = []
for i, a in enumerate(A):
  B.append(i+1+a)
  C.append(i+1-a)
cb = Counter(B)
cc = Counter(C)
ans = 0
for k, v in cb.items():
  ans += v*cc[k]
print(ans)

Solution (Python):


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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
count = 0
dicta = {}
for i in range(N):
    if i - A[i] in dicta:
        count += dicta[i-A[i]]
    if i + A[i] in dicta:
        dicta[i+A[i]] += 1
    else:
        dicta[i+A[i]] = 1
print(count)

Solution (Python):
from collections import defaultdict
import bisect
N = int(input())
A = list(map(int,input().split()))
plus = [A[i]+(i+1) for i in range(N)]
diff = [-A[i]+(i+1) for i in range(N)]

plusc = defaultdict(list)
for i in range(N):
    plusc[plus[i]].append(i)
ans = 0
for i in range(1,N):
    ans += bisect.bisect_left(plusc[diff[i]],i)
print(ans)

Solution (Python):
import sys
l = sys.stdin.readline().strip()
N = int(l)
l = sys.stdin.readline().strip() 
a = list(map(int, l.split()))
count = 0



L = []
R = []
for i, one in enumerate(a):
    L.append(one + i+1)
    if (i+1 - one) > 0:
        R.append(i+1-one)
L = sorted(L)
R_count = {}
for one in R:
    if one in R_count:
        R_count[one] += 1
    else:
        R_count[one] = 1
count = 0
for one in L:
    if one in R_count:
        count += R_count[one]

print(count)

Solution (Python):
from collections import defaultdict

n=int(input())
d=defaultdict(int)

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

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


ans=0

for i in range(n):
 d[i-a[i]]-=1
 ans+=d[a[i]+i]
 
print(ans)

Solution (Python):
from sys import stdin, stdout
import math
import bisect
import queue
from collections import defaultdict

n = int(stdin.readline().strip())
d1 = defaultdict(int)
d2 = defaultdict(int)
arr = list(map(int,stdin.readline().strip().split()))

cur = 1
ans = 0

for each in arr:
    d1[each + cur] += 1
    d2[cur - each] += 1
    cur += 1

for key, value in d1.items():
    ans += d2[key]*value

stdout.writelines(str(ans) + '\n')

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

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

def main():
  N = int(readline())
  line = readline().split()
  R = {}
  L = {}
  for i in range(N):
    r = int(line[i]) + i + 1
    l = i + 1 - int(line[i])
    if r in R:
      R[r] += 1
    else:
      R[r] = 1
    if l in L:
      L[l] += 1
    else:
      L[l] = 1
  cnt = 0
  for i in R:
    if i in L:
      cnt += R[i] * L[i]
  print(cnt)
  
if __name__ == '__main__':
  main()

Solution (Python):
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)

Solution (Python):
from collections import defaultdict
N = int(input())
ans = 0
d1 = defaultdict(int)
d2 = defaultdict(int)
for i, j in enumerate(map(int, input().split())):
    d1[i-j]+=1
    d2[i+j]+=1
for i in d1.keys():
    ans += d1[i]*d2[i]
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
l={}
r={}
for i in range(n):
    x=i+1+a[i]
    if x in l:
        l[x]+=1
    else:
        l[x]=1

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

ans=0
for i in l:
    if i in r:
        ans+=l[i]*r[i]
print(ans)

Solution (Python):
from math import sqrt
n = int(input())
arr = list(map(int,input().split()))
x = dict()
y = dict()
for i in range(n):
    key = i+arr[i]
    if(x.get(key,0) == 0):
        x[key] = [i]
    else:
        x[key].append(i)
    key = i - arr[i]
    if (y.get(key, 0) == 0):
        y[key] = [i]
    else:
        y[key].append(i)
ans = 0
for key in x:
    if(y.get(key,0)!=0):
        ans += len(y[key])*len(x[key])
print(ans)

Solution (Python):
import bisect
n = int(input())
*A, = map(int, input().split())
B = [A[i] + i for i in range(n)]
C = [i - A[i] for i in range(n)]
C.sort()
ans = 0
for b in B:
    ans += bisect.bisect_right(C, b) - bisect.bisect_left(C, b)
print(ans)

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

L = {}
R = {}
for i in range(N+1):
    L[i] = []  
    R[i] = []

for i in range(N):
    key = i+1+A[i]
    if key<N+1: L[key].append(i+1)
   
for i in range(N):
    key = i+1-A[i]
    if (key<N+1)and(0<=key): R[key].append(i+1)
    

ans=0
for x in range(N+1):
    ans+=len(L[x])*len(R[x])
print(ans)

Solution (Python):
from collections import defaultdict
 
N = int(input())	
 


L = defaultdict(lambda: 0)
R = defaultdict(lambda: 0)
for i, val in enumerate(map(int, input().split())):
  L[i - val] += 1	
  R[i + val] += 1	
  
cnt = 0
for key, val in L.items():
  if key > 0:
    cnt += L[key] * R[key]
    
print(cnt)

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
def main():
    n=int(input())
    A=list(map(int,input().split()))
    X,Y=[0]*n,[0]*n
    for i in range(n):
        X[i]=i+A[i]
        Y[i]=i-A[i]
    set_x=set(X)
    yc=Counter(Y)
    xc=Counter(X)
    ans=0
    for x in set_x:
        ans+=xc[x]*yc[x]
    print(ans)

if __name__=='__main__':
    main()