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 collections
n = int(input())
a = list(map(int, input().split()))

d = collections.defaultdict(int)
for i in range(n):
    d[i + 1 + a[i]] += 1

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

print (ans)

Solution (Python):
import sys
from collections import Counter
def input(): return sys.stdin.readline().rstrip()

def main():
    N = int(input())
    A = map(int, input().split())
    Aplus = [0] * N
    for i, a in enumerate(A):
        Aplus[i] = i + a + 1
    A_counter = Counter(Aplus)

    ans = 0
    for i in range(1, N + 1):
        key = i * 2 - Aplus[i-1]
        ans += A_counter[key]
        if key == Aplus[i - 1]:
            ans -= 1
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import collections
n = int(input())
a = [int(i) for i in input().split()]
pl = []
dic_mn = collections.defaultdict(int)

for i in range(n):
    pl.append(a[i] + i)
    dic_mn[i - a[i]] += 1
ans = 0
for i in range(n):
    ans += dic_mn[pl[i]]

print(ans)

Solution (Python):
import sys, math
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

N = ri()
A = rl()

Ap = [0] * N
Am = [0] * N

for i in range(N):
	Ap[i] = i + A[i]
	Am[i] = i - A[i]

import collections
h = collections.defaultdict(int)
ans = 0
for i in range(N):
	if Am[i] in h:
		ans += h[Am[i]]
	h[Ap[i]] += 1
print(ans)

Solution (Python):
import bisect
def main():
    N = int(input())
    A = [int(_) for _ in input().split()]
    B = [-a-i for i, a in enumerate(A)]
    C = sorted([a-i for i, a in enumerate(A)])
    D = [a+i for i, a in enumerate(A)]
    E = sorted([i-a for i, a in enumerate(A)])

    output = 0
    for b in B:
        x = bisect.bisect_left(C, b)
        y = bisect.bisect_right(C, b)
        output += y - x
    for d in D:
        x = bisect.bisect_left(E, d)
        y = bisect.bisect_right(E, d)
        output += y - x
    print(output//2)
    return

if __name__ == '__main__':
    main()

Solution (Python):
import sys
import collections

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


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


    c = collections.Counter()

    ans = 0
    for i, a in enumerate(A):
        ans += c[a - i]
        c[-a - i] += 1

    print(ans)




if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=[-1*A[i]+(i+1) for i in range(N)]
C=[A[i]+(i+1) for i in range(N)]
b={}
c={}
for i in range(N):
    if B[i] in b:
        b[B[i]]+=1
    else:
        b[B[i]]=1
for i in range(N):
    if C[i] in c:
        c[C[i]]+=1
    else:
        c[C[i]]=1
ans=0
for k in c.keys():
    if k in b:
        if k==0:
            a0+=1
        else:
            ans+=b[k]*c[k]
print(ans)

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

for i in range(N):
    key = A[i] + i
    if d.get(key) == None:
        d[key] = 1
    else:
        d[key] += 1
    
    key = i - A[i]
    if d.get(key) != None:
        ans += d[key]

print(ans)

Solution (Python):
N = int(input())
List=list(map(int,input().split()))
ans = [[0,0] for i in range(N+1)]
for i in range(N):
  a = List[i]
  if i + a < N:
    ans[i+a][0] += 1
  if i-a >= 0:
    ans[i-a][1] += 1
    
cnt = 0
for i in range(N+1):
  a = ans[i][0]
  b = ans[i][1]
  
  cnt = cnt + a*b
     
print(int(cnt))

Solution (Python):
n = int(input())
from collections import Counter
a = list(map(int,input().split()))
b = []
for i in range(n):
  b.append(a[i]-i-1)
b = Counter(b)
k = -a[0]-1
a.append(10**10)
ans = 0
for i in range(n):
  ans += b[k]
  k -= 1
  k += a[i]-a[i+1]
print(ans)

Solution (Python):
from collections import Counter


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




R = Counter()
cnt = 0
for n in range(N - 1, 0, -1):
    R[(n + 1) - A[n]] += 1
    c = A[n - 1] + ((n - 1) + 1)
    cnt += R[c]













print(cnt)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
B = [A[i]-(i+1) for i in range(N)]
C = sorted(B)
from bisect import bisect
from bisect import bisect_right
from bisect import bisect_left

ans=0

for i in range(N):
    target = -2*(i+1)-B[i]
    l=bisect_left(C, target)
    r=bisect_right(C, target)
    ans+=r-l
print(ans)

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

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

ans = 0

sa = [0]*(n+1)

for i in range(n):
    if i+1-t[i] > 0:
        sa[i+1-t[i]] += 1

for i in range(n):
    if 0 < i+1+t[i] < n:
        ans += sa[i+1+t[i]]
    if i+1-t[i] > 0:
        sa[i+1-t[i]] -= 1
print(ans)

Solution (Python):

n = int(input())
a = list(map(int,input().split()))
bucket_l = [0] * (2*n)
bucket_r = [0] * (2*n)
for i,v in enumerate(a):
    if v > n:
        continue
    if i-v >= 0:
        bucket_l[i-v]+=1
    bucket_r[i+v]+=1

ret = 0
for i,j in zip(bucket_l,bucket_r):
    ret+=i*j
print(ret)

Solution (Python):
import collections

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

P = [i+A[i] for i in range(N)]
Q = [j-A[j] for j in range(N)]

Q_dict = collections.Counter(Q)

cnt = 0
for i in range(N-1):
    Q_dict[Q[i]] -= 1
    cnt += Q_dict[P[i]]

print(cnt)

Solution (Python):

import math

import collections

def combinations_count(n, r):
    if n < r:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

    A_plus = []
    A_minus = []
    for i, a in enumerate(A):
        A_plus.append(a+i)
        A_minus.append(-a+i)
    
    plus = collections.Counter(A_plus)
    minus = collections.Counter(A_minus)
    ans = 0
    for k,v in plus.items():
        if k in minus.keys():
            ans += minus[k]*plus[k]
    print(ans)


if __name__ == "__main__":
    main()
"""
ans = 0
for i in range(2,N-1):
    k = 0
    while(k+i<N):
        
        if(A[k]+A[k+i]) == i:
            ans += 1
        k+=1
print(ans)
"""

Solution (Python):

from collections import defaultdict


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

    j_aj = [(i + 1) - a[i] for i in range(n)]
    j_aj_cnt = defaultdict(int)    
    for j in j_aj:
        j_aj_cnt[j] += 1

    ans = 0
    for i in range(n):
        j_aj_cnt[j_aj[i]] -= 1
        t = (i + 1) + a[i]
        ans += j_aj_cnt[t]
    print(ans)


submit()

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

D = defaultdict(int)
ans = 0
for i, a, b in zip(reversed(range(N)), A[::-1], B[::-1]):
    ans += D[-(i + a + 1)]
    D[b] += 1

print(ans)

Solution (Python):
n=int(input())
l=[0 for i in range(2*10**5+1)]
r=[0 for i in range(2*10**5+1)]
c=1
a=0
if n!=0:
  k=[int(i) for i in input().split()]
for i in range(n):
  if k[i]+c<=2*10**5:
    l[k[i]+c]+=1 
  if -k[i]+c>=0 and -k[i]+c<=2*10**5:
    r[-k[i]+c]+=1
  c+=1
for i in range(n):
  a+=l[i]*r[i]
print(a)

Solution (Python):
from collections import defaultdict

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

dp = defaultdict(int)
dm = defaultdict(int)
for i, ai in enumerate(a):
    if i + 1 + ai <= n:
        dp[i + ai + 1] += 1
    if 0 < i + 1 - ai <= n:
        dm[i + 1 - ai] += 1
ans = 0
keys = set(dp.keys()) & set(dm.keys())
for k in keys:
    ans += dp[k] * dm[k]
print(ans)

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


def RunLengthEncoding(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 = [i-a[i] for i in range(n)]
plus = [i+a[i] for i in range(n)]
minus_cnt = RunLengthEncoding(minus)
plus_cnt = RunLengthEncoding(plus)

cnt=0
for i in minus_cnt:
    if i in plus_cnt:
        cnt += minus_cnt[i] * plus_cnt[i]
print(cnt)

Solution (Python):
from collections import defaultdict

def main():
    N = int(input())
    A = list(map(int, input().split()))
    ans = 0
    B = [0] * N
    for i in range(N):
        B[i] = (i + 1) - A[i]
        A[i] += i + 1

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

    print(ans)

    


if __name__ == "__main__":
    main()

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

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

num_dict = dict()

ans = 0

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

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

num_dic = {}

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil
from operator import itemgetter
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
inf = 10**17
mod = 10**9 + 7


n=I()
h=LI()
plus=[]
minus=[]
for i in range(1,n+1):
    plus.append(i+h[i-1])
    minus.append(i-h[i-1])



ans=0
c=Counter(plus)
for m in minus:
    ans+=c[m]
print(ans)

Solution (Python):
def read_int():
    return int(input())

def read_int_map():
    return map(int,input().split(' '))

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

def read_s_list_loop(n):
    return [input() for _ in range(n)]

from bisect import bisect_right
from bisect import bisect_left
import copy

N = read_int()
An = read_int_list()

An_diff = [0]*N
for i,a in enumerate(An):
    An_diff[i] = a - i
An_store = copy.deepcopy(An_diff)
An_diff.sort()
count_map = {}
ans = 0
for i,a in enumerate(An):
    target = (a+i)*(-1)
    shoud_be_minus_cnt = An_store[i]
    if shoud_be_minus_cnt not in count_map:
        count_map[shoud_be_minus_cnt] = 1
    else:
        count_map[shoud_be_minus_cnt] += 1
    cnt = bisect_right(An_diff,target) - bisect_left(An_diff,target)
    if target in count_map:
        cnt -= count_map[target]
    ans += cnt
print(ans)

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

ans=0
for g in range(n):
    ans+=d1[a[g]-g]
    ans+=d2[a[g]+g]
    d1[-a[g]-g]+=1
    d2[-a[g]+1]+=1
print(ans)

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

Solution (Python):
from collections import defaultdict
def solve():
    N = int(input())
    A = list(map(int,input().split()))
    add = []
    counter = defaultdict(int)
    
    for i in range(N):
        add.append(i+1 + A[i])
        key = i+1 - A[i]
        
        counter[key] += 1
    
    ans = 0
    for add_value in add:
        ans += counter[add_value]
    
    print(ans)

if __name__ == '__main__':
    solve()

Solution (Python):
import bisect
N = int(input())
alist = list(map(int, input().split()))
alist = [0] + alist
sumlist = []

for i in range(1,N+1):
    sumlist.append(i+alist[i])
sumlist.sort()
ans = 0

for i in range(1, N+1):
    diff = i - alist[i]
    if diff <= 0:
        continue
    i = bisect.bisect_left(sumlist, diff)
    j = bisect.bisect_right(sumlist, diff)
    ans += j - i
print(ans)

Solution (Python):
import bisect
n=int(input())
l=list(map(int,input().split()))
dic={}
ans=0
for i in range(n):
    a=i-l[i]
    if a in dic.keys():
       dic[a].append(i)
    else:
       dic[a]=[i]
for i in range(n):
  a=i+l[i]
  if a in dic.keys():
    s=dic[a]
  else:
    continue
  k=bisect.bisect_right(s,i)
  ans+=(len(s)-k)
print(ans)

Solution (Python):

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




import numpy as np
from collections import Counter

a = np.array(a)
ap = a + np.arange(n)
am = a - np.arange(n)
am = -am

c = Counter(am)

s = 0
for k in range(n):
    s += c[ap[k]]
    c[am[k]] -= 1

print(s)

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

left = [i - a for i, a in enumerate(A_list)]
right = [i + a for i, a in enumerate(A_list)]

left_c = collections.Counter(left)
right_c = collections.Counter(right)

count = 0
for i in left_c:
    if i in right_c:
        count += left_c[i] * right_c[i]

print(count)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
B = [A[i] - i for i in range(n)]
C = [-A[i] - i for i in range(n)]



C.sort()
from bisect import *

ans = 0

for b in B:
    ans += bisect(C, b) - bisect_left(C, b)

print(ans)

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

from collections import defaultdict
ue=defaultdict(int)
st=defaultdict(int)

for i in range(N):
  ue[i+1+A[i]]+=1
  st[i+1-A[i]]+=1

ans=0

for i in ue.keys():
  ans+=ue[i]*st[i]
  
print(ans)

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

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

cnt = {}
ans = 0

for i in range(len(a)):
    add = (i + 1) + a[i]
    sub = (i + 1) - a[i]

    
    if sub in cnt:
        ans += cnt[sub]

    
    if add in cnt:
        cnt[add] += 1
    else:
        cnt[add] = 1

print(ans)

Solution (Python):
import collections
n=int(input())
l=list(map(int,input().split()))
a=[0]*n
b=[0]*n
for i in range(n):
  a[i]=i+1+l[i]
  b[i]=(i+1-l[i])
A=collections.Counter(a)
aa=A.most_common()
B=collections.Counter(b)

ans=0
for i in range(len(aa)):
  ans+=B[aa[i][0]]*aa[i][1]

print(ans)

Solution (Python):
import bisect

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


sumlist = []

for i in range(1,N+1):
    
    sumlist.append(i+alist[i])


sumlist.sort()





ans = 0

for i in range(1, N+1):
    diff = i - alist[i]
    if diff <= 0:
        continue
    i = bisect.bisect_left(sumlist, diff)
    j = bisect.bisect_right(sumlist, diff)
    ans += j - i
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+a)
  C.append(i-a)
  
b = Counter(B)
c = Counter(C)
ans=0
for d in b.items():
  ans+=d[1]*c[d[0]]
print(ans)

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

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


def serch(li, x):
    l = 0
    r = len(li)
    mid = 0
    while True:
        mid = (l + r) // 2
        if li[mid] == x:
            return True

        if l >= r:
            break

        if li[mid] < x:
            l = mid + 1
        else:
            r = mid

    return False


ma = []
for i, a in enumerate(A):
    ma.append(i + a)

cnt = collections.Counter(ma)

ans = 0
for i, a in enumerate(A):
    
    ans += cnt[i - a]

print(ans)

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

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

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

R.sort()
L.sort()

p = 0
q = 0

ans = 0
while p < N and q < N:
    if L[p] == R[q]:
        a = 1
        b = 1
        while p+a < N and L[p+a] == L[p]:
            a += 1
        while q+b < N and R[q+b] == R[q]:
            b += 1
        ans += a*b
        p += a
        q += b
    elif L[p] < R[q]:
        p += 1
    else:
        q += 1
    
    
    
    
    






print(ans)

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
from collections import *
c=Counter()
a=0
for i in range(n):
  c[i+l[i]]+=1
  if i>l[i]:
    a+=c[i-l[i]]
print(a)

Solution (Python):
import bisect

n = int(input())

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

plus_l = [i + a_l[i] for i in range(n)]
minus_l = [i - a_l[i] for i in range(n)]

minus_l = sorted(minus_l)

ans = 0

for i in range(n):
    cur = plus_l[i]
    left = bisect.bisect_left(minus_l, cur)
    right = bisect.bisect_right(minus_l, cur)

    if left >= n or minus_l[left] != cur:
        continue

    ans = ans + right - left

print(ans)

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

Solution (Python):
from collections import defaultdict
n = int(input())
ls = list(map(int,input().strip().split()))
dic1 = defaultdict(int)
dic2 = defaultdict(int)
for i,val in enumerate(ls,1):
    dic1[val+i] += 1
    dic2[val-i] += 1
ans = 0
for key,val in dic2.items():
    if -key in dic1:
        ans += val*dic1[-key]
print(ans)

Solution (Python):
from collections import defaultdict


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

plusA = A.copy()
minusA = A.copy()

for i in range(n):
    plusA[i] += i
    minusA[i] -= i

pldic = defaultdict(int)
midic = defaultdict(int)

for a, b in zip(plusA, minusA):
    pldic[a] += 1
    midic[b] += 1

ans = 0
keys = list(pldic.keys())
for key in keys:
    count = pldic[key]

    pair = midic[-key]
    ans += count * pair
print(ans)

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


def solve(N,A):
    ans = 0
    lis = [0]*(2*10**5)
    for i,a in enumerate(A):
        if -a-i >= -2*10**5:
            lis[-a-i]+=1
    for i,a in enumerate(A):
        if a-i < 0:
            ans += lis[a-i]
    return ans
print(solve(N,A))

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


from collections import defaultdict
ab_dic = defaultdict(lambda: 0) 


ab=[]


ba=[]

for i in range(n):
    banme=i+1
    ab.append(a[i]+banme)
    ba.append(banme-a[i])
    ab_dic[banme+a[i]]+=1


result=0
for i in range(n):
    ii=n-1-i
    if ab_dic[ba[ii]]>=1:
        result+=ab_dic[ba[ii]]
    ab_dic[ab[ii]]-=1


print(result)

Solution (Python):
def resolve():
    n = int(input())
    a = list(map(int, input().split()))
    l = [x - i for i, x in enumerate(a)]
    from collections import Counter
    c = Counter(l)
    ans = 0
    for i in range(n):
        index = -i * 2 - l[i]
        ans += c[index] if index in c else 0
        c[l[i]] -= 1
    print(ans)


if __name__ == '__main__':
    resolve()

Solution (Python):
n=input()
a=map(int,raw_input().split())
b=[]
d={}
for i in range(n):
    b+=[i+a[i]]
    if i+a[i] in d:
        d[i+a[i]]+=1
    else:
        d[i+a[i]]=1

c=0

for j in range(n-1,-1,-1):
    if (j-a[j]) in d:
        c+=d[(j-a[j])]
        
print c

Solution (Python):


def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    

    
    

    n = int(input())
    a = list(map(int, input().split()))
    res = 0
    l = [0]*(10**6)
    used = []
    for i in range(n):
        if 0<=i+1-a[i]<n:
            l[i+1-a[i]] += 1
            used.append(i+1-a[i])
    cnt = 0
    for i in range(n):
        if 0<=i+1-a[i]<n:
            l[used[cnt]] -= 1
            cnt += 1
        if 0<=i+1+a[i]<10**6:
            b = i+1+a[i]
            res += l[b]
    print(res)



if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
import bisect
B = [(i+A[i],i-A[i]) for i in range(N)]

B.sort(key=lambda x: x[1])
C = [B[i][1] for i in range(N)]

ans = 0
for i in range(N):
    idl = bisect.bisect_left(C,B[i][0])
    idr = bisect.bisect_right(C,B[i][0])
    
    if 0<=idl<idr<=N and C[idl]==C[idr-1]==B[i][0]:
        ans += idr-idl
print(ans)

Solution (Python):

import sys
from bisect import bisect_left,bisect_right

def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = [i+A[i] for i in range(N)] 
    B.sort()
    C = [i-A[i] for i in range(N)] 
    C.sort()
    ans = 0
    for i in range(N):
        ans += bisect_right(C,B[i]) - bisect_left(C,B[i])
    print(ans)

if __name__ == '__main__':
    main()

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

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


dic={}
for i in range(len(A)):
    if i-A[i] in dic:
        dic[i-A[i]]+=1
    else:
        dic[i-A[i]]=1

ans=0
for i in range(len(A)):
    temp=i+A[i]
    if temp in dic:
        ans+=dic[temp]
        
    if i-A[i]==i+A[i]:
        ans-=1
print(ans)

Solution (Python):
import sys, bisect
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N = I()
    A = LI()

    AA = list(sorted([i+1+A[i] for i in range(N)]))

    ans = 0
    for i in range(N):
        idx_l = bisect.bisect_left(AA, i+1-A[i])
        idx_r = bisect.bisect_right(AA, i+1-A[i])
        
        
        num = 0
        if 0<=idx_l<N and 0<=idx_r<N:
            num = idx_r - idx_l
        
        
        
        ans += num

    print(ans)

if __name__ == '__main__':
    resolve()

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

ans = 0
start_idx = [0] * N
for i, a in enumerate(A):
    if i+1+a < N:
        start_idx[i+1+a] += 1
    if i-a+1 >= 0:
        ans += start_idx[i-a+1]

print(ans)

Solution (Python):
def main():

    from bisect import bisect_left as bl, bisect_right as br, insort
    import sys,heapq,math
    from collections import defaultdict as dd, deque
    def data(): return sys.stdin.readline().strip()
    def mdata(): return list(map(int, data().split()))
    out = sys.stdout.write
    
    INF = int(1e9)
    mod = int(1e9)+7

    n=int(data())
    A=list(mdata())
    d1=dd(int)
    d2=dd(int)
    for i in range(n):
        if i+1+A[i]<n:
            d1[i+1+A[i]]+=1
        if i+1-A[i]>1:
            d2[i+1-A[i]]+=1
    ans=0
    for i in d1:
        ans+=d1[i]*d2[i]
    print(ans)

if __name__ == '__main__':
    main()

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

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

p = {}
q = {}
for i in range(n):
  pp = a[i]+i+1
  if pp in p:
    p[pp] += 1
  else:
    p[pp] = 1
  qq = -(a[i]-(i+1))
  if qq in q:
    q[qq] += 1
  else:
    q[qq] = 1
ans = 0
for kp,vp in p.items():
  if kp in q:
    vq = q[kp]
    ans += vp*vq
print(ans)

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
a = na()

from collections import defaultdict
d = defaultdict(int)
ans = 0

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

print(ans)

Solution (Python):
n = int(input())
a = [int(_) for _ in input().split()]
plus = [0] * n
minus = [0] * n
for i in range(n):
    plus[i] = i + a[i]
    minus[i] = i - a[i]



d = {}
for i in range((n)):
    if minus[i] in d:
        d[minus[i]] += 1
    else:
        d[minus[i]] = 1


ans = 0
for i in range(n):
    if plus[i] in d:
        ans += d[plus[i]]
print(ans)

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

Solution (Python):
n = int(input())
a = [int(val) for val in input().split()]
mp = dict()
ans = 0
for i in range(n):
    addval = a[i] + (i + 1)
    subval = (i + 1) - a[i]
    if subval in mp.keys():
        ans += mp[subval]
    mp[addval] = mp.get(addval, 0) + 1

print(ans)

Solution (Python):
from bisect import bisect_left
n=int(input())
a=list(map(int,input().split()))
num1=[0]*n
num2=[0]*n
for i in range(n):
    num1[i]=i+1+a[i]
    num2[i]=i+1-a[i]
num2_sort=sorted(list(set(num2)))
num2_set=set(num2)
num2_cnt=[0]*len(num2_sort)
for i in range(n):
    b=bisect_left(num2_sort,num2[i])
    num2_cnt[b]+=1
ans=0
for i in range(n):
    if num1[i] in num2_set:
        c=bisect_left(num2_sort,num1[i])
        ans+=num2_cnt[c]
    d=bisect_left(num2_sort,num2[i])
    num2_cnt[d]-=1
print(ans)

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
ans = 0
li1 = [0]*(n)
li2 = [0]*n
for i in range(n):
    if a[i]+i<n:
        li1[a[i]+i]+=1 
    if 0<=i-a[i]<n:
        li2[-a[i]+i]+=1
for i in range(n):
    ans+=li1[i]*li2[i]
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
INF = 10**10
MOD = 10**9 + 7
N = int(input())
A = list(map(int,input().split()))

minus = []
plus = []
for i in range(len(A)):
    minus.append(A[i]-(i+1))
    plus.append(-A[i]-(i+1))
minus.sort()
plus.sort()


minus.append(INF)
plus.append(INF)
i=0
j=0
ans=0
while(i!=N and j!=N):
    
    if minus[i] < plus[j]:
        i+=1
    elif minus[i] > plus[j]:
        j+=1
    else:
        ci = 1
        cj = 1
        
        while(minus[i+1] == minus[i]):
            i+=1
            ci += 1
        while(plus[j+1] == plus[j]):
            j+=1
            cj += 1
        i+=1
        j+=1
        ans += ci*cj
    
print (ans)

Solution (Python):
import bisect

N = int(input())

border = N * 2

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

b = [A[0] + 1]

C = [0] * (border + 1)

ans = 0

for i, a in enumerate(A, 1):
    if 0 <= i - a <= border:
        ans += C[i - a]
    if i + a <= border:
        C[i + a] += 1
    
    
    
    

print(ans)

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

count=0
wa={}
for j in range(1,n):
  if j+a[j-1] not in wa:
    wa[j+a[j-1]]=1
  else:
    wa[j+a[j-1]]+=1
  
  sa=j+1-a[j]
  if sa in wa:
    count+=wa[sa]

print(count)

Solution (Python):
from collections import Counter

N = int(input())
*A, = map(int, input().split())
A = [a-i for i, a in enumerate(A)]
S = Counter(A)
ans = 0
for i, a in enumerate(A):
    ans += S[-2*i-a]
print(ans)

Solution (Python):


def solve():
    left = {}
    right = {}
    for i in range(n):
        r = - a[i] + (i + 1)
        l = a[i] + (i + 1)
        if not str(r) in right:
            right[str(r)] = 1
        else:
            right[str(r)] += 1
        if not str(l) in left:
            left[str(l)] = 1
        else:
            left[str(l)]+= 1
    ans = 0
    for i in range(n):
        ii = str(i)
        if ii in right and ii in left:
            ans += right[ii] * left[ii]
    print(ans)
    return


n = int(input())
a = [int(s) for s in input().split(" ")]
solve()

Solution (Python):
from bisect import bisect_left

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

    b = [a[i] + i + 1  for i in range(n)]
    c = {}
    for i in range(n):
        cc = a[i] - i - 1
        if cc not in c:
            c[cc] = []
        c[cc].append(i)
    ans = 0

    for i in range(n):
        bb = -b[i]
        if bb not in c:
            continue
        idx = bisect_left(c[bb], i)
        ans += len(c[bb]) - idx
    print(ans)

if __name__ == '__main__':
    resolve()

Solution (Python):
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def S(): return sys.stdin.readline().rstrip()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)

MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**5+10)

N = I()
a = LI()
s = [0]*N
for i in range(N):
    if i-a[i]+1>=0:
        s[i-a[i]+1] += 1
ans = 0
for i in range(N):
    start = i+a[i]+1
    if start<N:
        ans += s[start]

print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
r=[[] for _ in range(n)]
l=[[] for _ in range(n)]
for i in range(n):
	if i+a[i]<n:
		r[i+a[i]].append(i)
	if i-a[i]>=0:
		l[i-a[i]].append(i)
cnt=0
for i in range(n):
	cnt+=len(l[i])*len(r[i])
print(cnt)

Solution (Python):
import bisect
N=int(input())
A=list(map(int,input().split()))
A1=[]
A2=[]
for i in range(N):
  A1.append(i+1+A[i])
  A2.append(i+1-A[i])
A1.sort()

ans=0
for i in range(N):
  ai=A[i]
  i+=1
  ans+=bisect.bisect_right(A1,i-ai)-bisect.bisect_left(A1,i-ai)
  
print(ans)