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 sys
from collections import Counter

n, *a = map(int, sys.stdin.read().split())

def main():
    b = [a[i] + i for i in range(n)]
    c = [i - a[i] for i in range(n)]
    cb = Counter(b)
    cc = Counter(c)
    tot = 0
    for v, c in cb.items():
        tot += c * cc[v]
    print(tot)
    
if __name__ == '__main__':
    main()

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

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

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

minus = [0] * (N-1)
for j in range(1, N):
  minus[j-1] = j - A[j]

sum = 0
c = dict(collections.Counter(minus))
for i in range(N-1):
  sum += c.get(plus[i], 0)
  c[minus[i]] -= 1

print(sum)

Solution (Python):
n,a=int(input()),list(map(int, input().split()))
d=[0]*n
def main():
 for i,x in enumerate(a):
  if i+x<n:d[i+x]+=1
 print(sum([d[i-x]for i,x in enumerate(a)if i-x>=0]))
main()

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)  
to_index = lambda x: int(x) - 1  
print_list_in_2D = lambda x: print(*x, sep="\n")  


def input_int():
    return int(input())

def map_int_input():
    return map(int, input())

MII = map_int_input

def MII_split():
    return map(int, input().split())

def MII_to_index():
    return map(to_index, input())

def MII_split_to_index():
    return map(to_index, input().split())


def list_int_inputs():
    return list(map(int, input()))

LII = list_int_inputs

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


def LII_2D(rows_number):
    return [LII() for _ in range(rows_number)]

def LII_split_2D(rows_number):
    return [LII_split() for _ in range(rows_number)]

N = input_int()
As = LII_split()

count = 0
ps = dict()
ms = dict()
for i in range(N):
    p = i + 1 + As[i]
    if p in ps:
        ps[p].append(i)
    else:
        ps[p] = [i]
    m = i + 1 - As[i]
    if m in ms:
        ms[m].append(i)
    else:
        ms[m] = [i]


for m, m_indicies in ms.items():
    if m not in ps:
        continue
    else:
        p_indicies = ps[m]
        count += len(p_indicies) * len(m_indicies)

print(count)

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

"""ナイーブ
count = 0
for i in range(n-1):
    for j in range(i, n):
        if i != j and abs(i-j) == a[i] + a[j]:
            count += 1
print(count)
"""


from collections import Counter
a_plus = [0]*n
a_minus = [0]*n
for i in range(n):
    a_plus[i] = i + a[i]
    a_minus[i] = i - a[i]
c1 = Counter(a_plus)
c2 = Counter(a_minus)


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

Solution (Python):
def main():
 n,a=int(input()),list(map(int, input().split()))
 d=[0]*n
 for i,x in enumerate(a):
  if i+x<n:d[i+x]+=1
 print(sum([d[i-x]for i,x in enumerate(a)if i-x>=0]))
main()

Solution (Python):
from sys import stdin
from collections import Counter


def get_result(data):
    N = data[0][0]
    A = data[1]
    L = [0 for _ in range(N)]
    R = [0 for _ in range(N)]
    for i in range(N):
        L[i] = i + A[i] + 1
        R[i] = i + 1 - A[i]

    L_cnt = Counter(L)
    R_cnt = Counter(R)
    ans = 0
    all_key = list(set(R_cnt.keys() & L_cnt.keys()))
    for key in all_key:
        ans += L_cnt[key] * R_cnt[key]
    return ans


if __name__ == '__main__':
    raw_data = [val.rstrip() for val in stdin.readlines()]
    data = [list(map(int, val.split(' '))) for val in raw_data]
    result = get_result(data)
    print(result)

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

Solution (Python):


def solve():
    N = int(input())
    As = list(map(int, input().split()))

    Ldic = {}
    for i, a in enumerate(As):
        if not (a+i) in Ldic:
            Ldic[a+i] = 0
        Ldic[a+i] += 1

    ans = 0
    for j, a in enumerate(As):
        if j-a in Ldic:
            ans += Ldic[j-a]

    print(ans)


if __name__ == "__main__":
    solve()

Solution (Python):
def main():
 n,a = int(input()),list(map(int, input().split()))
 d = [0]*n
 for i,x in enumerate(a):
  if i+x<n:d[i+x]+=1
 print(sum([d[i-x]for i,x in enumerate(a)if i-x>=0]))
main()

Solution (Python):
from sys import stdin
from collections import Counter


def get_result(data):
    N = data[0][0]
    A = data[1]
    L = [0 for _ in range(N)]
    R = [0 for _ in range(N)]
    for i in range(N):
        L[i] = i + A[i] + 1
        R[i] = i + 1 - A[i]

    L_cnt = Counter(L)
    R_cnt = Counter(R)
    ans = 0
    max_key = max(R_cnt.keys())
    for i in range(max_key+1):
        ans += L_cnt[i] * R_cnt[i]
    return ans


if __name__ == '__main__':
    raw_data = [val.rstrip() for val in stdin.readlines()]
    data = [list(map(int, val.split(' '))) for val in raw_data]
    result = get_result(data)
    print(result)

Solution (Python):
def main():
 n,a = int(input()),list(map(int, input().split()))
 d = [0]*n
 for i,x in enumerate(a):
  if i+x<n:d[i+x] += 1
 print(sum([d[i-x]for i,x in enumerate(a)if i-x>=0]))
main()

Solution (Python):
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)


inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""

from collections import defaultdict
import sys
sys.setrecursionlimit(10 ** 7)


def wi(): return list(map(int, sys.stdin.readline().split()))



def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


def si(): return int(sys.stdin.readline())  


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  


def grid(n): return [s_list() for _ in range(n)]


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def round_up(a, b):
    """ a/b の切り上げ """
    c = (a + b - 1) / b
    return c


def lcm(a, b):
    """a,bの最小公倍数"""
    return a * b // gcd(a, b)


def floor(a, b):
    """floor(a/b) 整数値を返す"""
    c = (a - a % b)/b
    return int(c)


def main():
    n = int(input())
    a = wi()
    L = defaultdict(int)
    R = defaultdict(int)
    for i in range(n):
        L[i + a[i]] += 1
        R[i - a[i]] += 1
    ans = 0
    for i in L:
        ans += L[i] * R[i]
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
r=[0]*(n-1)
t=[0]*(n)
dr={}
dt={l[0]:[0]}
for i in range(1,n):
  x=i-l[i]
  y=i+l[i]
  if x in dr:
    dr[x].append(i)
  else:
    dr[x]=[i]
  if y in dt:
    dt[y].append(i)
  else:
    dt[y]=[i]
ans=0
for x in dr.keys():
  if x>0 and x in dt:
    y=dr[x]
    z=dt[x]
    ans+=len(z)*len(y)
print(ans)

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

cnt_ind_minus_height = {}
cnt_height_plus_ind = {}
cnt_height_minus_ind = {}
cnt_minus_height_minus_ind = {}

for ind in range(n):
    height = hlist[ind]
    if ind - height not in cnt_ind_minus_height:
        cnt_ind_minus_height[ind - height] = 0
    cnt_ind_minus_height[ind - height] += 1

    if ind + height not in cnt_height_plus_ind:
        cnt_height_plus_ind[ind + height] = 0
    cnt_height_plus_ind[ind + height] += 1

    if height-ind not in cnt_height_minus_ind:
        cnt_height_minus_ind[height-ind] = 0
    cnt_height_minus_ind[height-ind] += 1

    if -ind - height not in cnt_minus_height_minus_ind:
        cnt_minus_height_minus_ind[-height-ind] = 0
    cnt_minus_height_minus_ind[-height-ind] += 1

ans = 0

for val in cnt_height_minus_ind:
    if val in cnt_minus_height_minus_ind:
        ans += cnt_height_minus_ind[val]*cnt_minus_height_minus_ind[val]


for val in cnt_height_plus_ind:
    if val in cnt_ind_minus_height:
        ans += cnt_height_plus_ind[val]*cnt_ind_minus_height[val]


print(ans//2)

Solution (Python):
def main():
  n = int(input())
  a = list(map(int, input().split()))
  d = [0]*n
  for i, x in enumerate(a):
    if i+x < n: d[i+x] += 1
  print(sum([d[i-x] for i, x in enumerate(a) if i-x >= 0]))
main()

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

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

Solution (Python):
def main():
  n = int(input())
  a = list(map(int, input().split()))
  d = [0]*n
  for i, x in enumerate(a):
    if i+x < n: d[i+x] += 1
  ans = 0
  for i, x in enumerate(a):
    if i-x >= 0: ans += d[i-x]
  print(ans)

if __name__ == "__main__":
  main()

Solution (Python):
def main():
  n = int(input())
  a = list(map(int, input().split()))
  d = [0]*n
  for i, x in enumerate(a):
    if i+x < n: d[i+x] += 1
  ans = 0
  for i, x in enumerate(a):
    if i-x >= 0: ans += d[i-x]
  print(ans)
main()

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
ans = 0
l = []
r = []
for i,j in enumerate(A):
    l.append(i+j)
    r.append(i-j)

b = Counter(r)
for i in l:
    ans += b.get(i,0)
print(ans)

Solution (Python):
from collections import Counter
import bisect


def make_divisors(n):
    divisors = []
    for i in range(1, int(n ** 0.5) + 1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n // i)

    
    return divisors


if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    negA = []
    for i, a in enumerate(A):
        negA.append(i - a)

    cnt_negA = Counter(negA)
    negA_keys = sorted(cnt_negA.keys())
    ans = 0
    for i in range(N):
        if i + A[i] in cnt_negA:
            ans += cnt_negA[i + A[i]]
    print(ans)

Solution (Python):
from collections import Counter
import bisect


def make_divisors(n):
    divisors = []
    for i in range(1, int(n ** 0.5) + 1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n // i)

    
    return divisors


if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    negA = []
    idx_negA = []
    for i, a in enumerate(A):
        negA.append(i - a)
        idx_negA.append(i)

    cnt_negA = Counter(negA)
    negA_keys = sorted(cnt_negA.keys())
    ans = 0
    for i in range(N):
        if i + A[i] in cnt_negA:
            ans += cnt_negA[i + A[i]]
    print(ans)

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

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

Solution (Python):
import sys

N = int(sys.stdin.readline().strip())
A = list(map(int, sys.stdin.readline().strip().split()))
 
d = [0]*N
ans = 0
for i in range(N):
    a = A[i]
    l,r = i+a, i-a
    if 0 <= l < N:
        d[l] += 1
    if 0 <= r < N:
        ans += d[r]
 
print(ans)

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

Solution (Python):


def main():
    n = int(input())
    a = list(map(int, input().split()))
    cnt = 0

    lst1 = [0] * n
    lst2 = [0] * n

    for i in range(1, n+1):
        p = i - a[i-1]
        q = i + a[i-1]

        if 0 <= p < n:
            lst1[p] += 1
        if 0 <= q < n:
            lst2[q] += 1

    for i in range(n):
        cnt += lst1[i] * lst2[i]

    print(cnt)

if __name__ == '__main__':
    main()

Solution (Python):
import collections

if __name__ == "__main__":
    N = int(input()) 
    
    A = list(map(int, input().rstrip().split(' ')))
    
    
    
    
    
    
    
    
    
    
    
    
    
    L = [i+A[i] for i in range(N)]
    R = [i-A[i] for i in range(N)]
    c_l = collections.Counter(L)
    c_r = collections.Counter(R)
    
    
    
    
    ans = 0
    for k, v in c_l.items():
        v_ = c_r.get(k, 0)
        ans += v * v_
    print(ans)

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

Solution (Python):
from collections import defaultdict

n = int(input())
a = list(map(int, input().split()))
ap = defaultdict(int)
for i in range(n):
    ap[i - a[i]] += 1

ans = 0
for i in range(n):
    ans += ap[a[i] + i]
print(ans)

Solution (Python):
from _collections import defaultdict
import sys

read = sys.stdin.buffer.read
N = int(input())
A = [int(i) for i in input().split()]
D = defaultdict(int)

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

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

Solution (Python):
import time
startTimeProblem=time.time()

import fileinput, sys, itertools, functools, copy
from math import *
from bisect import *
from heapq import *
from collections import *


def lcm(a, b):  
    return (a*b)/gcd(a, b)

class InputHelper:
    def __init__(self):
        self.myinput = fileinput.input()

    def isLocal(self):
        return not fileinput.isstdin()

    def int(self):
        return int(self.myinput.readline().rstrip())

    def ints(self):
        return [int(_) for _ in self.myinput.readline().rstrip().split()]

    def str(self):
        return self.myinput.readline().rstrip()

    def strs(self):
        return [_ for _ in self.myinput.readline().rstrip().split()]

class OutputHelper:
    def int(self, a):
        print(a)    

    def ints(self, a):  
        print(" ".join([str(_) for _ in a]))
    
    def intsNL(self, a):
        for _ in a:
            print(_)
    
    def str(self, s):
        print(s)

    def strs(self, s):
        print(" ".join([_ for _ in s]))

    def strsNL(self, s):
        for st in s:
            print(st)

class ListNode:
    def __init__(self, val):
        self.val = val
        self.next = None
        self.prev = None

In = InputHelper()
Out = OutputHelper()



N = In.int()
A = In.ints()
drueb = [defaultdict(int) for i in range(N)]

for i in range(N):
    
    
    val = i+1
    drueb[-1][A[i]-val] += 1

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

    if j<len(drueb):
        if -A[i]-(i+1) in drueb[-1]:
            ans+=drueb[-1][-A[i]-(i+1)]
    
Out.int(ans)
        


if len(sys.argv)>2 and sys.argv[2]=="TIMEIT":
    fin = (time.time()-startTimeProblem)*1000
    print("{:.2f}".format(fin) + "ms")

Solution (Python):
n = int(input())
A=list(map(int,input().split()))
cnt=0
B=[]
C=[]
for i,a in enumerate(A):
    B.append(A[i]+i)
    C.append(A[i]-i)
from collections import Counter
Bd=dict(Counter(B))
Cd=dict(Counter(C))
cnt=0
for k in Bd.keys():
    b=Bd.get(k)
    c=Cd.get(-k)
    if b and c:
        cnt+=b*c
print(cnt)

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

d = [0]*N
ans = 0
for i in range(N):
    a = A[i]
    l,r = i+a, i-a
    if 0 <= l < N:
        d[l] += 1
    if 0 <= r < N:
        ans += d[r]

print(ans)

Solution (Python):
import sys
import array

if __name__ == "__main__":
    N = int(sys.stdin.readline().strip())
    A = array.array('i', [int(a) for a in sys.stdin.readline().strip().split(" ")])

    count_pair = 0
    L = {}
    R = {}

    for i in range(N):
        l = A[i] + (i + 1)
        r = (i + 1) - A[i]

        if l in L:
            L[l].append(i+1)
        else:
            L[l] = [i+1]

        if r in R:
            R[r].append(i+1)
        else:
            R[r] = [i+1]

    print(sum([len(L[m]) * len(R[m]) for m in list(set(L.keys()) & set(R.keys()))]))

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

Solution (Python):
import sys
def inpl(): return list(map(int, sys.stdin.readline().split()))
n = int(sys.stdin.readline())
a = inpl()
dic ={}
res = 0
for i in range(n):
    res += dic.get(i - a[i], 0)
    dic[i + a[i]] = dic.get(i + a[i], 0) + 1

print(res)

Solution (Python):
n = int(input())
A=list(map(int,input().split()))
cnt=0
B=[]
C=[]
for i,a in enumerate(A):
    B.append(A[i]+i)
    C.append(A[i]-i)
from collections import Counter
Bd=dict(Counter(B))
Cd=dict(Counter(C))
cnt=0
for k in Bd.keys():
    try:
        cnt+=Bd[k]*Cd[-k]
    except KeyError:
        continue
print(cnt)

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

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


cnt_height_plus_ind = {}
cnt_ind_minus_height = {}

for ind in range(N):
    height = heights[ind]

    if ind - height not in cnt_ind_minus_height:
        cnt_ind_minus_height[ind - height] = 0
    cnt_ind_minus_height[ind - height] += 1

    if ind + height not in cnt_height_plus_ind:
        cnt_height_plus_ind[ind + height] = 0
    cnt_height_plus_ind[ind + height] += 1

ans = 0
for val in cnt_height_plus_ind:
    if val in cnt_ind_minus_height:
        ans += cnt_height_plus_ind[val] * cnt_ind_minus_height[val]

print(ans)

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

N = int(input())

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


cnt_height_plus_ind = {}
cnt_height_minus_ind = {}
cnt_ind_minus_height = {}
cnt_minus_height_minus_ind = {}

for ind in range(N):
    height = heights[ind]

    if ind - height not in cnt_ind_minus_height:
        cnt_ind_minus_height[ind - height] = 0
    cnt_ind_minus_height[ind - height] += 1

    if ind + height not in cnt_height_plus_ind:
        cnt_height_plus_ind[ind + height] = 0
    cnt_height_plus_ind[ind + height] += 1

    if height - ind not in cnt_height_minus_ind:
        cnt_height_minus_ind[height - ind] = 0
    cnt_height_minus_ind[height - ind] += 1

    if - ind - height not in cnt_minus_height_minus_ind:
        cnt_minus_height_minus_ind[-ind-height] = 0
    cnt_minus_height_minus_ind[-ind-height] += 1
    

ans = 0




for val in cnt_height_plus_ind:
    if val in cnt_ind_minus_height:
        ans += cnt_height_plus_ind[val] * cnt_ind_minus_height[val]

print(ans)

Solution (Python):
import numpy as np
from collections import Counter
n = int(input())
aa = np.array(list(map(int, input().split())))
cnt = 0
pl = aa + np.array(range(n))
ms = -aa + np.array(range(n))
common = set(pl).intersection(set(ms))

dic_pl = Counter(pl)
dic_ms = Counter(ms)

for k in dic_ms.keys():
    cnt += dic_ms[k] * dic_pl.get(k,0)
print(cnt)

Solution (Python):
def resolve():
    n=int(input())
    a = list(map(int, input().split()))
    b=[0]*n
    c=[0]*n
    ans=0
    for i in range(n):
        b[i]=a[i]+i
    for i in range(n):
        c[i]=i-a[i]
    bc=set(b).intersection(set(c))
    bd={}
    cd={}
    for i in bc:
        bd[i]=0
    for j in b:
            if j in bc:
                bd[j]+=1
    for i in bc:
        cd[i]=0
    for j in c:
            if j in bc:
                cd[j]+=1
    ansb=list(bd.values())
    ansc=list(cd.values())
    for k in range(len(bc)):
        ans+=ansb[k]*ansc[k]
    print(ans)
resolve()

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

N = int(input())

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


cnt_height_plus_ind = {}
cnt_height_minus_ind = {}
cnt_ind_minus_height = {}
cnt_minus_height_minus_ind = {}

for ind in range(N):
    height = heights[ind]

    if ind - height not in cnt_ind_minus_height:
        cnt_ind_minus_height[ind - height] = 0
    cnt_ind_minus_height[ind - height] += 1

    if ind + height not in cnt_height_plus_ind:
        cnt_height_plus_ind[ind + height] = 0
    cnt_height_plus_ind[ind + height] += 1

    if height - ind not in cnt_height_minus_ind:
        cnt_height_minus_ind[height - ind] = 0
    cnt_height_minus_ind[height - ind] += 1

    if - ind - height not in cnt_minus_height_minus_ind:
        cnt_minus_height_minus_ind[-ind-height] = 0
    cnt_minus_height_minus_ind[-ind-height] += 1
    

ans = 0
for val in cnt_height_minus_ind:
    if val in cnt_minus_height_minus_ind:
        ans += cnt_height_minus_ind[val] * cnt_minus_height_minus_ind[val]

for val in cnt_height_plus_ind:
    if val in cnt_ind_minus_height:
        ans += cnt_height_plus_ind[val] * cnt_ind_minus_height[val]

print(ans//2)

Solution (Python):
import numpy as np



import sys
input = sys.stdin.readline

from numba import njit


@njit
def loop(N,A):
    L = np.array([0 for i in range(N+1)])
    R = np.array([0 for i in range(N+1)])

    for i in range(N):
        temp = A[i]
        if i+1+temp <= N:
            L[i+1+temp] += 1
        if i+1-temp >= 0:
            R[i+1-temp] += 1

    return np.sum(L*R)

def main():
    N = int(input())
    A = list(map(int,input().split()))
    A = np.array(A)

    res = loop(N,A)

    print(res)




main()

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

ans = 0
dic = {}
for i in range(N):
    ans += dic.get(i - A[i], 0)
    dic[i + A[i]] = dic.get(i + A[i], 0) + 1
    
    
    
    

print(ans)

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

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

Solution (Python):
import sys
import collections

sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline


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


hiku = []

tasu = []

for i, a in enumerate(A, 1):
    hiku.append(i - a)
    tasu.append(i + a)

hiku_c = collections.Counter(hiku)
tasu_c = collections.Counter(tasu)

ans = 0
for key, value in tasu_c.items():
    if key in hiku_c.keys():
        ans += value * hiku_c[key]

print(ans)

Solution (Python):

from collections import Counter

def main():
    N = int(input())
    A = tuple(int(c) for c in input().split(' '))
    L = [i+a for i, a in enumerate(A)]
    R = [i-a for i, a in enumerate(A)]
    
    

    
    L_t = Counter(L)
    R_t = Counter(R)
    cnt = 0
    x_candidate = set(L) & set(R)
    for x in x_candidate:
        l_candidate = L_t[x]
        r_candidate = R_t[x]
        cnt += l_candidate * r_candidate

    print(cnt)

if __name__ == "__main__":
    main()

Solution (Python):
import numpy as np



import sys
input = sys.stdin.readline



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

    INF = 10 ** 9

    L = np.array([0 for i in range(N+1)])
    R = np.array([0 for i in range(N+1)])

    for i in range(N):
        temp = A[i]
        if i+1+temp <= N:
            L[i+1+temp] += 1
        if i+1-temp >= 0:
            R[i+1-temp] += 1

    res = np.sum(L*R)

    print(res)




main()

Solution (Python):
from collections import defaultdict
N = int(input())
A = list(map(int, input().split()))
cnt1 = defaultdict(int)
cnt2 = defaultdict(int)
for i in range(N):
    cnt1[A[i] + i + 1] += 1
    cnt2[i + 1 - A[i]] += 1
ans = 0
for k, v in cnt1.items():
    ans += v * cnt2[k]
print(ans)

Solution (Python):

N=int(input())
A=[int(x) for x in input().split()]
X={}
Y={}
for i in range(N):
  L=i+A[i]
  R=i-A[i]
  X[L]=X[L]+1 if L in X else 1
  Y[R]=Y[R]+1 if R in Y else 1
ans=0
for n in X:
  if n in Y:
    ans+=X[n]*Y[n]
print(ans)

Solution (Python):
import sys
import collections

sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline


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

left = []
right = []

for i, a in enumerate(A, 1):
    left.append(i - a)
    right.append(i + a)

left_counter = collections.Counter(left)
right_counter = collections.Counter(right)

ans = 0
for i, j in right_counter.items():
    if i in left_counter:
        ans += j * left_counter[i]

print(ans)

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

Solution (Python):
from collections import defaultdict

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

ans = 0
dic = defaultdict(int)

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

for i,a in enumerate(A):
    ans += dic[a+i]

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
data1 = [0 for _ in range(n)]
data2 = [0 for _ in range(n)]
for i,x in enumerate(a):
    if i+x <= n-1:
        data1[i+x] += 1
    if i-x >= 0:
        data2[i-x] += 1

ans = 0
for i in range(n):
    ans += data1[i]*data2[i]
print(ans)

Solution (Python):



import math
import itertools
from collections import defaultdict
from collections import Counter
import numpy as np

import heapq
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


n=int(input())

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

l=defaultdict(int)
r=defaultdict(int)
for i in range(n):
    ii=i+1
    l[ii+a[i]]+=1
    r[(ii-a[i])]+=1


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

print(ans)

Solution (Python):
from collections import defaultdict
N=int(input())
src=list(map(int,input().split()))
ans=0
d=defaultdict(int)
e=[]
for i,s in enumerate(src,1):
  l=i+s
  r=i-s
  d[l]+=1
  ans+=d[r]

print(ans)

Solution (Python):

import sys

import collections



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

    
    
    
    
    
    
    
    
    

    L = []  
    R = []  
    for i in range(N):
        x = y = i + 1
        L.append(x + A[i])
        R.append(y - A[i])
    cl = collections.Counter(L)
    cr = collections.Counter(R)
    ans = 0
    for i in range(N):
        if i in cl and i in cr:
            ans += cl[i] * cr[i]
    print(ans)
    return

if __name__ == '__main__':
    main()

Solution (Python):
from math import *
n=int(input())
a=[int(x) for x in input().split()]

f=[]
for i in range(n):
	f.append(0)
for i in range(n):
	if a[i]<n and (i+1)>a[i]:
		f[(i+1)-a[i]]+=1


ans=0
for i in range(n):
	
	if a[i]+i+1<n:
		ans+=f[a[i]+(i+1)]
print(ans)

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

d = [0]*N
ans = 0
for i in range(N):
    a = A[i]
    l,r = i+a, i-a
    if 0 <= l < N:
        d[l] += 1
    if 0 <= r < N:
        ans += d[r]

print(ans)

Solution (Python):
import sys
from collections import Counter


def _ia(): return map(int, sys.stdin.readline().strip().split())


def main():
    _ = sys.stdin.readline()
    a = list(_ia())
    l = Counter(ai+i+1 for i, ai in enumerate(a))
    r = Counter(i+1-ai for i, ai in enumerate(a))
    return sum(v*r.get(k, 0) for k, v in l.items())


if __name__ == "__main__":
    print(main())

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

d = {}
for i, ai in enumerate(a):
    l = ai + i+1
    if l in d:
        d[l] += 1
    else:
        d[l] = 1

p = 0
for i, ai in enumerate(a):
    r = i+1 - ai
    p += d.get(r, 0)

print(p)

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
res = 0
ia = []
ja = []
dia={}
dja={}
for i in range(n):
  dia[i+a[i]] = dia.get(i+a[i],0)+1
  dja[i-a[i]] = dja.get(i-a[i],0)+1
ans = 0
for di,nmi in dia.items():
  nmj = dja.get(di,0)
  ans += nmi*nmj

print(ans)

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

A1, A2 = {}, {}
for i, a in enumerate(A):
    key1 = str(-a-i)
    if key1 in A1:
        A1[key1] = (key1 in A1) * A1[key1] + 1
    else:
        A1[key1] = 1
    key2 = str(a-i)
    if key2 in A2:
        A2[key2] += 1
    else:
        A2[key2] = 1

cnt = 0
for key, value in A1.items():
    if key in A2:
        cnt += value * A2[key]

print(cnt)

Solution (Python):
from collections import Counter

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

iA = [i+n for i, n in enumerate(A)]
jA = [i-n for i, n in enumerate(A)]
dict1 = Counter(iA)
dict2 = Counter(jA)

ans = 0
for key, value in dict1.items():
    ans += value * dict2[key]
print(ans)

Solution (Python):
from sys import stdin, setrecursionlimit
from collections import defaultdict

setrecursionlimit(10 ** 9)


def input():
    return stdin.readline().strip()


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

d = defaultdict(int)
for i, a in enumerate(A, 1):
    d[i + a] += 1

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

print(ans)