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())
arr=list(map(int,input().split()))
ans=0
cnt=collections.defaultdict(int)
for i in range(1,n+1):
	ans+=cnt[i-arr[i-1]] 
	cnt[i+arr[i-1]]+=1 
print(ans)

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

s = defaultdict(lambda :0)
b = defaultdict(lambda :0)
for i,a in enumerate(A):
    s[i-a] = s[i-a] + 1
    b[i+a] = b[i+a] + 1

c = 0
for k in s:
    if k in b:
        c += b[k]*s[k]

print(c)

Solution (Python):
import sys
from collections import Counter
n = int(input())
a_ls = [int(i) for i in sys.stdin.readline().split()]
minus_a_ct = Counter([-a-i for i,a in enumerate(a_ls)])
a_ct = Counter([a-i for i,a in enumerate(a_ls)])
_sum = 0
for k, v in a_ct.items():
    _sum += v * minus_a_ct[k]
print(_sum)

Solution (Python):
import bisect


def main():
    n = int(input())
    a = [int(v) for v in input().split()]
    count = {}
    for i, v in enumerate(a):
        v = i - v
        if count.get(v) is None:
            count[v] = [i]
        else:
            count[v].append(i)

    ans = 0
    for i, v in enumerate(a):
        t = count.get(v+i)
        if t is None:
            continue
        idx = bisect.bisect_right(t, i)
        ans += len(t) - idx

    print(ans)


if __name__ == '__main__':
    main()

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

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

ans = 0

sum_dic = {}

for i in range(N-1):
    s = i + A_array[i]
    if s in sum_dic:
        sum_dic[s] += 1
    else:
        sum_dic[s] = 1
    t = A_array[i+1] - (i+1)
    if -t in sum_dic:
        ans += sum_dic[-t]


print(ans)

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
l = [0] * n
h = [0] * n

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

ans = 0
for ll, hh in zip(l, h):
    ans += ll * hh

print(ans)

Solution (Python):
def resolve():
    from collections import defaultdict
    N = int(input())
    A = [int(i) for i in input().split()]
    L = defaultdict(int)
    ans = 0
    for i in range(N):
        L[A[i] + i] += 1
        ans += L[i - A[i]]
    print(ans)


resolve()

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

l = {}
r = {}

for i, j in enumerate(a):
	if i - j in l:
		l[i-j] += 1
	else:
		l[i-j] = 1
	if i + j in r:
		r[i+j] += 1
	else:
		r[i+j] = 1
ans = 0
for i, j in l.items():
	if i in r:
		ans += r[i] * j

print(ans)

Solution (Python):
n=int(input())
a=map(int,input().split())
pulasu_i=dict()
mainasu_i=dict()
for i,num in enumerate(a):
    x=i+1
    if num+x not in pulasu_i:
        pulasu_i[num+x]=1
    else:
        pulasu_i[num+x]+=1
    if num-x not in mainasu_i:
        mainasu_i[num-x]=1
    else:
        mainasu_i[num-x]+=1

ans=0
for num,cnt in pulasu_i.items():
    if -num not in mainasu_i:
        continue
    else:
        ans+=cnt*mainasu_i[-num]
print(ans)

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
L = {}
R = {}
ans = 0
for i,j in enumerate(A):
    if i+j in L:
        L[i+j] += 1
    else:
        L[i+j] = 1
    if i-j in R:
        R[i-j] += 1
    else:
        R[i-j] = 1
for i,j in L.items():
    if i in R:
        ans += j*R[i]
print(ans)

Solution (Python):
import math
def fact(n):
    ans = 1
    for i in range(2, n+1):
        ans*= i
    return ans
def comb(n, c):
    return fact(n)//(fact(n-c)*c)

n = int(input())
nums = list(map(int, input().split()))
ans = 0
l = {}
r = []
for i in range(1,n+1):
    if(i + nums[i-1] not in l):
        l[i+nums[i-1]] =0
    l[i+nums[i-1]]+=1
    if((-1*nums[i-1])+i in l):
        ans+=l[(-1*nums[i-1])+i]
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):
    ans += cnt[i+1 - a]
    cnt[i+1 + a] += 1
print(ans)

Solution (Python):
from collections import defaultdict
dic = defaultdict(int)

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

j_arr =[]

ans = 0
for i in range(N):
    dic[i+arr[i]] += 1
    j_arr.append(-1*arr[i]+i)
    ans += dic[-1*arr[i]+i]

print(ans)

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

dic = {}
j_arr =[]

ans = 0
for i in range(N):
    if i+arr[i] not in dic:
        dic[i+arr[i]] = 0
    dic[i+arr[i]] += 1
    j_arr.append(-1*arr[i]+i)
    if -1*arr[i]+i in dic:
        ans += dic[-1*arr[i]+i]

print(ans)

Solution (Python):
from collections import defaultdict

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

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

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

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

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    plus = [None]*n
    minus = [None]*n
    for i, ai in enumerate(a, 1):
        plus[i-1] = i+ai
        minus[i-1] = i-ai
    plus.sort()
    minus.sort()
    ans = 0
    j = 0
    i = 0
    while i < n:
        pi = plus[i]
        p_num = 1
        m_num = 1
        while j < n-1 and minus[j] < pi:
            j += 1
        mi = minus[j]
        if mi == pi:
            while j < n-1 and mi == minus[j+1]:
                m_num += 1
                j += 1
            while i < n-1 and pi == plus[i+1]:
                p_num += 1
                i += 1
            ans += p_num*m_num
        i += 1
        
    print(ans)
    

if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
dic = {}
ans = 0
A = list(map(int, input().split()))
for i in range(N):
    dic[i] = 0

for i in range(N):
    b = A[i]+N-i-1
    if A[i] < N-i:
        a = N-i-A[i]-1
        dic[a] += 1
    if b < N:
        ans += dic[b]
print(ans)

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


def main():
    n = int(readline().rstrip())
    A = list(map(int, readline().rstrip().split()))
    ans = 0
    c = collections.Counter()

    for i in range(n):
        ans += c[i + 1 - A[i]]
        c[i + 1 + A[i]] += 1

    print(ans)


if __name__ == '__main__':
    main()

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

Solution (Python):
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

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

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  
  

  a=[]
  b={}
  for i in range(n):
    x=l[i]
    a.append(x+i)
    y=i-x
    if y in b:
      b[y]+=1
    else:
      b[y]=1

  ans=0
  for x in a:
    if x in b:
      ans+=b[x]

  return ans


print(main())

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






counter = 0
dic = {}
for j in range(N):
    if j-A[j] in dic:
        counter += dic[j - A[j]]
    if j + A[j] in dic:
        dic[j+A[j]] += 1
    else:
        dic[j+A[j]] = 1
print(counter)

Solution (Python):
import collections

n = int(input())
arr = list(map(int,input().split()))
ans = 0
cnt = collections.defaultdict(int)

for i in range(1, n+1):
  ans += cnt[i - arr[i-1]]
  cnt[i + arr[i-1]]+=1
print(ans)

Solution (Python):
from collections import Counter

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







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

S = set(Ap) & set(Am)

Cp = Counter(Ap)
Cm = Counter(Am)

cnt = 0
for i in S:
  cnt += Cp[i]*Cm[i]
  
print(cnt)

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

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
l =[]
r =[]
for id,h in enumerate(A,start=1):
    r.append(id -h)
    l.append(id +h)

r_cou =Counter(r)
l_cou =Counter(l)
ans =[]
for i in l_cou.keys():
    ans.append(l_cou[i] *r_cou.get(i,0))

print(sum(ans))

Solution (Python):
from collections import *

N = int(input())
A = list(map(int, input().split()))
ans = 0
c = Counter()

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

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):
from collections import defaultdict
n=int(input())
a=list(map(int,input().split()))
d=defaultdict(int)
for i in range(n):
    d[i+1-a[i]]+=1
print(sum(d[i+1+a[i]] for i in range(n)))

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+1-a[i]]+=1
ans=0
for i in range(n):
    ans+=d[i+1+a[i]]
print(sum(d[i+1+a[i]] for i in range(n)))

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

Solution (Python):
from collections import defaultdict
import bisect
def f():
    return []

d=defaultdict(f)
n=int(input())
a=list(map(int,input().split()))
x=[i+1+a[i] for i in range(n)]
y=[i+1-a[i] for i in range(n)]
for i in range(n):
    d[y[i]].append(i)

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

Solution (Python):
from collections import defaultdict

n = int(input())
a = list(map(int, input().split()))
dd = defaultdict(int)
ans = 0
for i, num in enumerate(a, start = 1):
    dd[i + num] += 1
for i, num in enumerate(a, start = 1):
    ans += dd[i - num]
print(ans)

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
L = [-1 for _ in range(N)]
R = [-1 for _ in range(N)]

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




LC = Counter(L)
RC = Counter(R)

ans = 0
for k,v in LC.items():
  if (-1) * k in RC:
    ans += v * RC[(-1)*k]
    
print(ans)

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
L = [-1 for _ in range(N)]
R = [-1 for _ in range(N)]

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




LC = Counter(L)
RC = Counter(R)

ans = 0
for k,v in LC.items():
  if (-1) * k in RC:
    ans += v * RC[(-1)*k]
    
print(ans)

Solution (Python):
def solve(n, a):
    S, T = {}, {}
    for i in range(n):
        b = a[i] + i
        if not b in S:
            S[b] = 0
        S[b] += 1

        c = -a[i] + i
        if not c in T:
            T[c] = 0
        T[c] += 1
    ans = 0
    for x, num in S.items():
        if x in T:
            ans += S[x] * T[x]
    return ans

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

Solution (Python):
import sys
from functools import lru_cache
from collections import defaultdict
inf = float('inf')
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**6)
def input(): return sys.stdin.readline().rstrip()
def read():
  return int(readline())
def reads():
  return map(int, readline().split())
x=read()
a=list(reads())
dic=[]
dic2=defaultdict(int)
for i in range(x):
  dic.append(i+a[i])
  dic2[i-a[i]]+=1
ans=0

for i in dic:
  ans+=dic2[i]
print(ans)

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    minus = {}
    plus = {}
    i = 0
    for n in A:
        i += 1
        if -n - i in plus:
            plus[-n - i] += 1
        else:
            plus[-n - i] = 1
        if n - i in minus:
            minus[n - i] += 1
        else:
            minus[n - i] = 1

    count = 0
    for p in plus:
        if p in minus:
            count += plus[p] * minus[p]
    print(count)

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(N, A)

if __name__ == '__main__':
    main()

Solution (Python):


from collections import Counter, deque, OrderedDict,defaultdict













import unittest
from io import StringIO
import sys
sys.setrecursionlimit(10 ** 5 + 10)


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


def resolve():

    N = int(input())
    A = list(map(int, input().split()))
    d=defaultdict(int)
    cnt=0
    for k,v in enumerate(A):
        d[k+v]+=1
        cnt+=d[k-v]

    print(cnt)


resolve()

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

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

c = {}
for i in range(n):
    if a[i] + i + 1 not in c.keys():
        c[a[i]+i+1] = 1
    else:
        c[a[i]+i+1] += 1



ans = 0
for i in range(n):
    if i + 1 - a[i] not in c.keys():
        pass
    else:
        ans += c[i + 1 - a[i]]

print(ans)

Solution (Python):
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n=ni()
a=nl()

ans=0

nums = dict()

for i in range(n):
  num_i = i+a[i]
  if num_i not in nums:
    nums[num_i]=0
  nums[num_i]+=1

for j in range(n):
  num_j = j-a[j]
  if num_j in nums:
    ans+=nums[num_j]

print(ans)

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


D=[0]*1000000
E=[0]*1000000
 
ans=0
 
for i in range(N):
  if A[i]<500000:
  
  
  
  	D[A[i]+i]+=1
  	E[-A[i]+i]+=1
   

for i in range(1000000):
  
  ans+=D[i]*E[i]
 
print(ans)

Solution (Python):
import sys


def main():
    read = lambda: sys.stdin.readline().rstrip()
    import collections
    
    d = collections.defaultdict(int)
    N = int(read())
    A = list(map(int,read().split()))

    cnt = 0
    for i, x in enumerate(A):
        d[i + x] += 1
        if d[i-x] > 0: cnt += d[i-x]
    
    print(cnt)
    


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    from collections import defaultdict
    n, *a = map(int, open(0).read().split())
    d = defaultdict(lambda: [0, 0])

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

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    from collections import defaultdict
    n, *a = map(int, open(0).read().split())
    d = defaultdict(lambda: {'R': 0, 'L': 0})

    for i, x in enumerate(a, 1):
        d[i + x]['R'] += 1
        d[i - x]['L'] += 1

    ans = 0
    for key, dic in d.items():
        l = dic['L']
        r = dic['R']
        ans += l * r

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import heapq
import re
from heapq import heapify, heappop, heappush
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt, ceil
from functools import lru_cache, reduce
INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def dijkstra_heap(s, edge, n):
    
    d = [10**20] * n
    used = [True] * n 
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr


def lcm(x, y):
    return (x * y) // gcd(x, y)


def lcm_list(numbers):
    return reduce(lcm, numbers, 1)


def gcd_list(numbers):
    return reduce(gcd, numbers)


def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True



def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P


def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret


n = int(input())
a = list(map(int, input().split()))
d = {}
ans = 0
for i in range(n):
    if i > 0 and d.get(a[i] - i) != None:
        ans += d.get(a[i] - i)

    if d.get(-i-a[i]) == None:
        d[-i-a[i]] = 1
    else:
        d[-i-a[i]] += 1
print(ans)

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

D = dict()
ans = 0
for i, a in enumerate(A):
    val = i - a
    if val in D.keys():
        ans += D[val]
        
    val = i + a
    if val not in D.keys():
        D[val] = 0
    D[val] += 1
    
print(ans)

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

print(ans)

Solution (Python):
from collections import Counter

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

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

res = 0
for k,v in S1.items():
  if S2[k] > 0:
    res += v*S2[k]

print(res)

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

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate, product, combinations_with_replacement
import sys
import bisect
import string
import math
import time

def I(): return int(input())
def S(): return input()
def MI(): return map(int, input().split())
def MS(): return map(str, input().split())
def LI(): return [int(i) for i in input().split()]
def LI_(): return [int(i)-1 for i in input().split()]
def StoI(): return [ord(i)-97 for i in input()]
def ItoS(nn): return chr(nn+97)
def input(): return sys.stdin.readline().rstrip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False



def main():
    N = I()
    A = LI()
    plus_Ai_minus_i = defaultdict(int)
    minus_Aj_minus_j = defaultdict(int)

    for i in range(len(A)):
        plus_Ai_minus_i[A[i] - i] += 1
        minus_Aj_minus_j[-A[i] - i] += 1

    ans = 0

    for k, cnt1 in plus_Ai_minus_i.items():
        cnt2 = minus_Aj_minus_j[k]
        ans += cnt1 * cnt2

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n = int(input())
    a = list(map(int,input().split()))
    sa,wa = {},{}
    for i in range(n):
        if a[i]+i+1 not in wa.keys():
            wa[a[i]+i+1]=1
        else:
            wa[a[i]+i+1]+=1
        if i-a[i] >0:
            if i+1-a[i] not in sa.keys():
                sa[i+1-a[i]]=1
            else:
                sa[i+1-a[i]]+=1
    ans = 0
    for k in sa.keys():
        if k in wa.keys():
            ans += sa[k]*wa[k]
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys

from collections import Counter



def main():
    N = int(input())
    As = [int(s) for s in input().split()]

    i_plus_As = [(i+1)+As[i] for i in range(N)]
    counter = Counter(i_plus_As)

    ans = 0
    for i in range(N):
        i_value = ( i + 1 )- As[i]
        if i_value <= 0:
            continue

        ans += counter[i_value]

    print(ans)

    return 0

if __name__ == '__main__':
    sys.exit(main())

Solution (Python):

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

    H = [0]*N
    for i in range(0,N):
        if i >= A[i]:
            H[i-A[i]] += 1

    ans = 0
    for k in range(0,N):
        Bk = k+A[k]
        if Bk < N:
            ans += H[Bk]

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
import bisect
import numpy as np
import math
from collections import deque
from collections import defaultdict
from math import gcd

N = int(input())
A = list(map(int, input().split()))
left = defaultdict(int)
right = defaultdict(int)
for i in range(N):
    num = i + 1
    left[A[i]+num] += 1
    right[-A[i]+num] += 1

ans = 0
for k, v in left.items():

    ans += right[k]*v

print(ans)

Solution (Python):
from collections import Counter

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

r = [(i+1)+a_i for i, a_i in enumerate(A)]
l = [(j+1)-a_j for j, a_j in enumerate(A)]
nr = Counter(r)
nl = Counter(l)

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

print(ans)

Solution (Python):
from collections import Counter
n = int(input())
a = [int(i) for i in input().split(" ")]
r = [i+1-j for i, j in enumerate(a)]
l = [j+i+1 for i, j in enumerate(a)]
nl = Counter(l)
nr = Counter(r)
 
ans = 0
for k, v in nl.items():
    ans += nr[k]*v
print(ans)

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

dic1 = {}
dic2 = {}

sum = 0

for i in range(N):
    v = i-A[i]
    if v < 0:
        continue
    if not v in dic1:
        dic1[v] = 0
    dic1[v] += 1

for i in range(N):
    v = i+A[i]
    if not v in dic2:
        dic2[v] = 0
    dic2[v] += 1


for k, v in dic1.items():
    if not k in dic2:
        continue
    sum += dic1[k]*dic2[k]

print(sum)

Solution (Python):
import sys
from io import StringIO
import unittest

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

    
    
    
    
    
    
    
    
    
    

    
    
    
    
    
    

    a_plus_dict = {}
    a_minus_dict = {}
    for i in range(len(a_s)):
        plus = a_plus_dict.get(i+1 + a_s[i], 0)
        a_plus_dict[i+1 + a_s[i]] = plus + 1

        if i+1 <= a_s[i]:
            continue

        minus = a_minus_dict.get(i+1 - a_s[i], 0)
        a_minus_dict[i+1 - a_s[i]] = minus + 1

    ans = 0
    for key, val in a_plus_dict.items():

        target = a_minus_dict.get(key, 0)
        ans += target * val

    print(ans)

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """6
2 3 3 1 3 1"""
        output = """3"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """6
5 2 4 2 8 8"""
        output = """0"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """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"""
        output = """22"""
        self.assertIO(input, output)

if __name__ == "__main__":
    
    resolve()

Solution (Python):
from collections import defaultdict

N = int(input())
p = list(map(int, input().split()))
d = defaultdict(int)
ans = 0

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

print(ans)

Solution (Python):




from collections import defaultdict

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

L=defaultdict(int)
R=defaultdict(int)

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

r=0
for k,v in L.items():
	j=R[k]
	r+=v*j

print(r)

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

cnt_c=defaultdict(int)
ans=0
for i in range(n):
  v=cnt_c[b[i]]
  ans+=v
  cnt_c[c[i]]+=1
print(ans)

Solution (Python):




from bisect import bisect_left

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

    L={}
    R={}

    res=0

    for i in range(N):
        l=(i+1)-A[i]
        r=A[i]+i+1
        if (l in R.keys()):
            res+=len(R[l])
        if (r in R.keys()):
            R[r].append(i+1)
        else:
            R[r]=[i+1]


    print(res)

if __name__=="__main__":
    main()

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
X, Y = [], []
for i in range(N):
    X.append(i + 1 + A[i])
    Y.append(i + 1 - A[i])
XL = Counter(X)
c = 0
for y in Y:
    c += XL.get(y, 0)
print(c)

Solution (Python):
import sys
import math
sys.setrecursionlimit(10**9)

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

    dic = [{},{}]
    counted = [set(),set()]

    def count(x,type):
        type = type - 1
        if x <= 0:
            return

        if not x in counted[type]:
            dic[type][x] = 1
            counted[type].add(x)
        else:
            dic[type][x] += 1


    for i in range(N):
        type1 = (i+1)-A[i]
        type2 = (i+1)+A[i]

        count(type1,1)
        count(type2,2)

    ans = 0
    
    for index,cnt in dic[0].items():
        if index in counted[1]:
            ans += cnt * dic[1][index]


    print(ans)



if __name__ == "__main__":
  main()

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

def int1(x): return 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 SI(): return input().split()

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

from math import ceil, floor, log2
from collections import deque
from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product


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

    memo = {}
    ans = 0
    for i in range(n):
        mae = (i+1) + A[i]
        ima = (i+1) - A[i]
        ans += memo.get(ima, 0)
        memo[mae] = memo.get(mae, 0) + 1
    
    print(ans)


if __name__ == '__main__':
    solve()