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

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



dict1 = {}
for i in range(1, N + 1):
    tmp = i + A[i - 1]
    if tmp not in dict1:
        dict1[tmp] = 1
    else:
        dict1[tmp] += 1

dict2 = {}
for i in range(1, N + 1):
    tmp = i - A[i - 1]
    if tmp not in dict2:
        dict2[tmp] = 1
    else:
        dict2[tmp] += 1


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

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

    L={}
    R={}

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

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

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

Solution (Python):
import sys


def main():
    input = sys.stdin.buffer.readline
    n = int(input())
    a = list(map(int, input().split()))
    b = [0] * (10 ** 6)
    ans = 0
    for i in range(n):
        if i - a[i] > 0:
            ans += b[i - a[i]]
        if i + a[i] < 10 ** 6:
            b[i + a[i]] += 1
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
L = list(map(int, input().split()))
import copy
U = copy.copy(L)
for i in range (0, N):
	L[i]+=(i+1)
	U[i]= -U[i]+(i+1)


L = sorted(L)
U = sorted(U)

import bisect

count = 0
for i in range (0, N):
	count+= bisect.bisect_right(U, L[i])-bisect.bisect_left(U, L[i])
    
print(count)

Solution (Python):
from collections import defaultdict
b = defaultdict(int)
c = defaultdict(int)
n = int(input())
a = map(int, input().split())

for idx,i in enumerate(a, start=1):
  b[i - idx] += 1
  c[i + idx] += 1
ans = 0
for k,v in b.items():
  ans += (v * c[-1 * k])
print(ans)

Solution (Python):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from collections import defaultdict
def main():
    n = int(input())
    a = list(map(int, input().split()))

    d1 = defaultdict(int)
    d2 = defaultdict(int)
    for i, ae in enumerate(a):
        d1[i + ae +1] += 1
        d2[i - ae +1] += 1
    r = 0
    for d1e in d1.keys():
        rt = d1[d1e] * d2[d1e]
        r += rt
    print(r)

if __name__ == '__main__':
    main()

Solution (Python):



def main():
    from collections import defaultdict
    import sys
    input = sys.stdin.readline

    n = int(input())
    a = list(map(int, input().split()))
    d = defaultdict(int)
    ans = 0

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

        
        summed = i + a[i]
        d[summed] += 1

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):



def main():
    from collections import defaultdict
    import sys
    input = sys.stdin.readline

    n = int(input())
    a = list(map(int, input().split()))
    d = defaultdict(int)
    ans = 0

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

        summed = i + a[i]
        d[summed] += 1

    print(ans)


if __name__ == '__main__':
    main()

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

s = [i+a for i, a in enumerate(A)]
t = [j-a for j, a in enumerate(A)]

d = Counter(s)
ans = 0

for i in t:
    ans += d[i]
print(ans)

Solution (Python):
from collections import defaultdict

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

d = defaultdict(int)

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

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

print(ans)

Solution (Python):
import sys
from collections import defaultdict

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    L = defaultdict(int)
    R = defaultdict(int)
    for i in range(N):
        L[i+1+A[i]] += 1
        R[i+1-A[i]] += 1
    ans = 0
    for key, value in L.items():
        ans += value*R[key]
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys,collections
input = sys.stdin.readline
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)
cnt = 0
Bc = collections.Counter(B)
Cc = collections.Counter(C)

for key,val in Bc.items():
    cnt += val*Cc[key]
print(cnt)

Solution (Python):

from collections import defaultdict

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

ctr = defaultdict(int)
ans = 0
for i in range(N):
    ctr[i + X[i]] += 1
    ans += ctr[i - X[i]]

print(ans)

Solution (Python):
from collections import defaultdict

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

ans = 0

for i in range(n):
    Sum = A[i] + i
    dic[Sum] += 1
    Dif = i - A[i]
    ans += dic[Dif]
print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b1,b2,dic,ans = [],[],{},0
for i, j in enumerate(a):
    b1.append(j-i)
    b2.append(-j-i)
for i, j in zip(b1, b2):
    if i in dic:
        ans += dic[i]
    if j in dic:
        dic[j] += 1
    else:
        dic[j] = 1
print(ans)

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

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

p = {}
m = {}
for i in range(N):
    if L[i] not in p:
        p[L[i]] = 1
    else:
        p[L[i]] += 1
for i in range(N):
    if R[i] not in m:
        m[R[i]] = 1
    else:
        m[R[i]] += 1

lower = max(min(p), min(m))
upper = min(max(p), max(m))
ans = 0
for x in range(lower, upper + 1):
    if x in p and x in m:
        ans += p[x] * m[x]

print(ans)

Solution (Python):
import collections

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




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

print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
ans = 0
p = {}
m = {}
for i in range(N):
    if p.get(i+A[i]) is None:
        p[i+A[i]] = 1
    else:
        p[i+A[i]] += 1
for i in range(N):
    if m.get(i-A[i]) is None:
        m[i-A[i]] = 1
    else:
        m[i-A[i]] += 1
for x in range(min(p),max(m)+1):
    if x in p and x in m:
        ans += p[x] * m[x]
print(ans)

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

Solution (Python):
import collections
N = int(input())
A = [0]
A += list(map(int, input().split()))
cnt = 0
L = [- 10 ** 6]
R = [- 10 ** 6]

for i in range(1, N + 1):
    L.append(i + A[i])
    R.append(i - A[i])
c = collections.Counter(L)

for j in range(1, N + 1):
    cnt += c.get(R[j], 0)
print(cnt)

Solution (Python):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter
from operator import mul
import copy


import heapq

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

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

pp = Counter(p)
qq = Counter(q)

ans = 0
for d in pp.items():
    ans += d[1] * qq[d[0]]

print(ans)

Solution (Python):
n = int(input())
arr = list(map(int,input().split()))
addarr = [i+1+arr[i] for i in range(n)]
diffarr = [i+1-arr[i] for i in range(n)]

from collections import Counter
cnt = Counter([addarr[0]])
ans = 0

for i in range(1,n):
    tmp = diffarr[i]
    ans += cnt[tmp]
    cnt.update([addarr[i]])

print(ans)

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


ans = 0
for k, v in dAl.items():
    ans += v * dAr[k]
print(ans)

Solution (Python):
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
import math
from copy import deepcopy
import queue


Mod = 1000000007
fact = [1, 1]
factinv = [1, 1]
inv = [0, 1] 
for i in range(2, 10**5 + 1):
    fact.append((fact[-1] * i) % Mod)
    inv.append((-inv[Mod % i] * (Mod // i)) % Mod)
    factinv.append((factinv[-1] * inv[-1]) % Mod)
    
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n - r] % p
    
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[y] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)
def pow_k(x, n):

    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2

    return K * x
def main():  
    n = int(input())
    a = list(map(int, input().split()))
    d = defaultdict(int)
    ans = 0
    for i in range(n):
        l = i - a[i]
        ans += d[l]
        d[a[i] + i] += 1
        
    print(ans)
if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter

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

p = [i+Ai for i, Ai in zip(range(1, N+1), A)]
q = [j-Aj for j, Aj in zip(range(1, N+1), A)]

pc = Counter(p)
qc = Counter(q)

r = sum(pc[k]*qc[k] for k in pc.keys() & qc.keys())

print(r)

Solution (Python):

import collections
import sys

input = sys.stdin.readline

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




l = [j - aj for j, aj in enumerate(a, 1)]
r = [i + ai for i, ai in enumerate(a, 1)]
cnt = collections.Counter(r)
ans = 0
for i in l:
    ans += cnt[i]
print(ans)

Solution (Python):
from collections import Counter

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


xc = Counter()
yc = Counter()
for i, v in enumerate(a):
    xc[i + v] += 1
    yc[i - v] += 1
ans = 0
for v in xc:
    ans += xc[v] * yc[v]
print(ans)

Solution (Python):
from collections import defaultdict

def main():
    n = int(input())
    a = list(map(int, input().split()))
    d = defaultdict(lambda : 0)
    ans = 0
    for i, ai in enumerate(a, 1):
        num = str(ai+i)
        d[num] += 1
        ans += d[str(i-ai)]
    print(ans)
        

if __name__ == "__main__":
    main()

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

x = [ aa + i  for i,aa in enumerate(a,start = 1)]
y = [ i - aa  for i,aa in enumerate(a,start = 1)]

from collections import Counter
c = Counter(y)
ans = 0
for xx in x:
    ans += c[xx]
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
start = [0]*N
ans = 0
for i in range(N):
    if i+A[i]+1 < N:
        start[i+A[i]+1] += 1

for i in range(N):
    if i-A[i]+1 < 0:
        continue
    if i-A[i]+1 >= N:
        continue
    ans += start[i-A[i]+1]

print(ans)

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

l_count = defaultdict(int)
r_count = defaultdict(int)
for i, A in enumerate(As):
    l_count[i + A] += 1
    r_count[i - A] += 1
    
ans = 0
for k, lv in l_count.items():
    rv = r_count[k]
    ans += lv * rv

print(ans)

Solution (Python):
import sys
from bisect import bisect_right, bisect_left
sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    A_i, A_j = [], []
    for i in range(n):
        A_i.append(-A[i] - (i + 1))

    for j in range(n):
        A_j.append(A[j] - (j + 1))

    A_j.sort()

    res = 0
    for a in A_i:
        idx_1 = bisect_left(A_j, a)
        idx_2 = bisect_right(A_j, a)
        res += idx_2 - idx_1
    print(res)
    

if __name__ == '__main__':
    resolve()

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

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

Solution (Python):
from collections import defaultdict

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

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

print(ans)

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

L = [a+i for a, i in enumerate(A, 1)]
R = [a-i for a, i in enumerate(A, 1)]

ans = 0
R_c = Counter(R)
for l in L:
    ans += R_c[l]

print(ans)

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

sum_list = [i + A[i] for i in range(1, N+1)]
sum_list.sort()

count = 0
for i in range(1, N+1):
  v = i - A[i]
  idx_right = bisect.bisect_right(sum_list, v)
  idx_left = bisect.bisect_left(sum_list, v)
  if idx_right > 0 and sum_list[idx_right-1] == v:
    count += idx_right - idx_left
print(count)

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

if __name__ == "__main__":
    main()

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

d=defaultdict(int)
sm=0
for i in range(n):
  d[i+a[i]]+=1
  sm+=d[i-a[i]]
print(sm)

Solution (Python):
from collections import Counter

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

l1=[i+a[i] for i in range(n)]
l2=[i-a[i] for i in range(n)]

c2=Counter(l2)

ans=0
for i in l1:
  ans+=c2[i]

print(ans)

Solution (Python):
from collections import Counter

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

l1=[i+a[i] for i in range(n)]
l2=[i-a[i] for i in range(n)]

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

ans=0
for i in c1:
  ans+=c1[i]*c2[i]

print(ans)

Solution (Python):
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))
from collections import Counter

n=int(input())
a=lnii()

l1=[i+a[i] for i in range(n)]
l2=[i-a[i] for i in range(n)]

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

ans=0
for i in c1:
  ans+=c1[i]*c2[i]

print(ans)

Solution (Python):

from collections import defaultdict
from bisect import bisect_left

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

ctr = defaultdict(list)
for i in range(N):
    ctr[i + A[i]].append(i)

ans = 0
for i in range(N):
    v = i - A[i]
    if v in ctr:
        ans += bisect_left(ctr[v], i)

print(ans)

Solution (Python):
import numpy as np
from collections import Counter
n = int(input())
a = np.array(list(map(int, input().split())))
ni = np.array([i for i in range(n)])

ap = a + ni
an = (a - ni) * -1

cp = Counter(ap)
cn = Counter(an)

ans = 0
for i in cp.keys():
  ans += cp[i] * cn[i] 

print(ans)

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

ans = 0
dic1 = {}
dic2 = {}
for i in range(n):
    tmp1 = a[i] + (i + 1)
    tmp2 = -a[i] + (i + 1)
    if tmp1 in dic1:
        dic1[tmp1] += 1
    else:
        dic1[tmp1] = 1
    if tmp2 in dic2:
        dic2[tmp2] += 1
    else:
        dic2[tmp2] = 1
for key in dic1.keys():
    if key in dic2:
        ans += dic1[key] * dic2[key]
print(ans)

Solution (Python):
import sys 
from collections import Counter
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
A = list(map(int,readline().split()))
X = [0]*N
Y = [0]*N
for i in range(N):
    X[i] = (i+1) + A[i]
    Y[i] = (i+1) - A[i]  
cnt = Counter(Y)
ans = 0
for i in range(N):
    ans += cnt[X[i]] 
    cnt[Y[i]] -= 1
print(ans)

Solution (Python):


N = int(input().strip())
A_list = list(map(int, input().rstrip().split()))


plus_h = {}
minus_h = {}

for i in range(N):
    p = (i+1) + A_list[i]
    m = (i+1) - A_list[i]
    
    plus_h.setdefault(p, 0)
    plus_h[p] += 1
    
    minus_h.setdefault(m, 0)
    minus_h[m] += 1


cnt = 0

for k in plus_h:
    if k in minus_h:
        cnt += (plus_h[k] * minus_h[k])


print(cnt)

Solution (Python):
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
ma = max(A) + n -1 
P = [i+a+1 for i,a in enumerate(A) if i+a+1 <= ma]
Q = [j-a+1 for j,a in enumerate(A) if j-a+1 > 0]
 
cQ = Counter(Q)
cP = Counter(P)
ans = 0
for p in cP.keys():
    ans += cQ[p]*cP[p]
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ans = 0
B = []
C = []
maA = max(A) + N -1
for k in range(N):
  if A[k] + k <= maA:
    B.append(A[k]+k)
  if -A[k] + k >0:
    C.append(-A[k]+k)
B.sort()
C.sort()

c = 0
b = 0
while b < len(B) and c < len(C):
  if B[b] == C[c]:
    s = 0
    t = 0
    j = float('inf')
    k = float('inf')
    for j in range(b, len(B)):
      if B[b] == B[j]:
        s += 1
      else:
        break
    for k in range(c, len(C)):
      if C[c] == C[k]:
        t +=1
      else:
        break
    ans += s*t
    b = j
    c = k
    continue
  elif B[b] > C[c]:
    c += 1
  else:
    b += 1


print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ans = 0
B = []
C = []
maA = max(A) + N -1
for k in range(N):
  if A[k] + k <= maA:
    B.append(A[k]+k)
  if -A[k] + k >0:
    C.append(-A[k]+k)
B.sort()
C.sort()

c = 0
b = 0
while b < len(B) and c < len(C):
  if B[b] == C[c]:
    s = 0
    t = 0
    j = float('inf')
    k = float('inf')
    for j in range(b, len(B)):
      if B[b] == B[j]:
        s += 1
      else:
        break
    for k in range(c, len(C)):
      if C[c] == C[k]:
        t +=1
      else:
        break
    ans += s*t
    b = j
    c = k
    continue
  elif B[b] > C[c]:
    c += 1
  else:
    b += 1


print(ans)

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

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

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

Solution (Python):




from collections import Counter


def main():
    N = int(input())
    A = list(map(int, input().split()))
    count = 0
    r = []
    l = []
    for i in range(N):
        r += i - A[i],
        l += i + A[i],

    c = Counter(l)
    for i in r:
        count += c[i]

    print(count)
main()

Solution (Python):
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
b=[]
c=[]
ans=0
for i,j in enumerate(a):
    b.append(i+j)
    c.append(i-j)
d=Counter(c)
for i in b:
    ans+=d.get(i,0)

print(ans)

Solution (Python):
def getInts():
    return [ int(x) for x in input().split() ]

[N] = getInts()
As = getInts()
A = {}
for i in range(1, N+1):
    A[i] = As[i-1]

L = {}
R = {}
for i in range(1, N+1):
    if i - A[i] not in L:
        L[i-A[i]] = 1
    else:
        L[i-A[i]] += 1
    if i + A[i] not in R:
        R[i+A[i]] = 1
    else:
        R[i+A[i]] += 1
count = 0
for k in R.keys():
    if k in L:
        count += R[k]*L[k]
print(count)

Solution (Python):
import collections
import bisect
n = int(input())
cnt = collections.defaultdict(list)
dat = list(map(int, input().split()))
buf = []
for i in range(n):
    cnt[i - dat[i]].append(i)

res = 0
for i in range(n):
    x = i + dat[i]
    if x in cnt:
        res += len(cnt[x]) - bisect.bisect_left(cnt[x], x)
print(res)

Solution (Python):
from collections import defaultdict

def main():
    n = int(input())
    a = list(map(int, input().split()))
    d = defaultdict(lambda: 0)
    ans = 0
    for i, ai in enumerate(a, 1):
        d[str(i+ai)] += 1
        ans += d[str(i-ai)]
    print(ans)
        

if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter
def main():
    n = int(input())
    A = list(map(int, input().split()))
    L = Counter([i + a for i, a in enumerate(A)])
    R = Counter([i - a for i, a in enumerate(A)])
    ans = 0
    for key in R.keys():
        ans += L[key] * R[key]
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict
N = int(input())
List = list(map(int,input().split()))
dicL = defaultdict(int)
dicR = defaultdict(int)
for i in range(N):
  dicL[List[i]+i] += 1
  dicR[i - List[i]] += 1
counter = 0
for item in dicL:
  if item in dicR:
    counter += dicL[item]*dicR[item]
print(counter)

Solution (Python):
def main():
    n = int(input())
    h = [int(i) for i in input().split()]

    d1 = { }
    d2 = { }
    for j in range(n):
        dif = j - h[j]
        tot = j + h[j]
        if dif not in d1:
            d1[dif] = 0
        if tot not in d2:
            d2[tot] = 0

        d1[dif] += 1
        d2[tot] += 1

    npairs = 0
    
    
    for i in range(n):
        dif = i - h[i]
        tot = i + h[i]
        if tot in d1:
            npairs += d1[tot]
        if dif in d2:
            npairs += d2[dif]

    print(npairs // 2)

main()

Solution (Python):

import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n = INT()

a = LIST()

first = a[:]

dd = defaultdict(int)

for i in range(n):
    first[i] = first[i]+(i+1)
    dd[first[i]]+=1

ans = 0

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

Solution (Python):
n = int(input())
A = [0]
A.extend(list(map(int,input().split())))
B = {}
pair = 0

for i in range(1,n+1):
  if i+A[i] in B:
    B[i+A[i]] += 1
  else:
    B[i+A[i]] = 1
  
for j in range(2,n+1):
  if j-A[j] in B:
    pair += B[j-A[j]]

print(pair)

Solution (Python):
import collections

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

b = [0] * n
c = [0] * n
for i in range(n):
  b[i] = a[i] + (i + 1)
  c[i] = (i + 1) - a[i]

ans = 0
cCc = collections.Counter(c)

for k, v in collections.Counter(b).items():
  if v != 0 and k in cCc:
    
    ans += v * cCc[k]

print(ans)

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

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

B.sort()
C.sort()

j = 0
ct = 0
cashe = 0
for i in range(N):
  if i >= 1 and B[i-1] == B[i]:
    ct += cashe
  else:
    cashe = 0
    while C[j] <= B[i]:
      if C[j] == B[i]:
        ct += 1
        cashe += 1
      j += 1
      
print(ct)

Solution (Python):

N = int(input())
A = list(map(int,input().split()))
from collections import defaultdict
AB = defaultdict(int)
_AB = defaultdict(int)
for i,a in enumerate(A):
    AB[i+1+A[i]]+=1
    _AB[-A[i]+i+1]+=1
ans = 0
for key,val in AB.items():
    ans += val*_AB[key]
print(ans)

Solution (Python):


"""
変数分離すれば互いに独立なので、
連想配列でO(N)になる。
"""


import sys
input = sys.stdin.readline
from collections import Counter

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

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

dic = Counter(P)

res = 0
for m in M:
    res += dic.get(m,0)

print(res)

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

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

Ld = {}
Rd = {}

for i in range(N):
    if R[i] in Rd.keys():
        Rd[R[i]] += 1
    else:
        Rd[R[i]] = 1

ans = 0
for i in range(N):
    if L[i] in Rd.keys():
        ans += Rd[L[i]]
    if L[i] == R[i]:
        ans -= 1
        
print(ans)

Solution (Python):

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

Solution (Python):
n = int(input())
t = list(map(int, input().split()))
r = {}
ans = 0
for i in range(len(t)):
  try:
    r[i+1 + t[i]] += 1
  except KeyError:
    r[i+1 + t[i]] = 1
for i in range(len(t)):
  if((i+1 - t[i]) >= 1):
    try:
      ans += r[i+1-t[i]]
    except KeyError:
      continue
print(ans)

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

if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
A = [0]+list(map(int,input().split()))

d = {}

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

Solution (Python):
import sys
def Ii():return int(sys.stdin.buffer.readline())
def Mi():return map(int,sys.stdin.buffer.readline().split())
def Li():return list(map(int,sys.stdin.buffer.readline().split()))

n = Ii()
a = Li()
x = [0]*n
ans = 0
for i in range(n): 
  if a[i]+i < n:
    x[a[i]+i] += 1
  if i-a[i] >= 0:
    ans += x[i-a[i]]
      
print(ans)

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

dic = {}
for i in range(N):
    data = i + 1 + A[i]
    if data in dic:
        dic[data] += 1
    else:
        dic[data] = 1
        
ans = 0
for i in range(N):
    res = i + 1 - A[i]
    if res in dic:  ans += dic[res]
print(ans)

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

ans = 0
d = defaultdict(int)
cur = N
for a in A[::-1]:
    ans += d[cur + a]
    if cur - a > 0:
        d[cur - a] += 1
    cur -= 1
print(ans)

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

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

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

L = [A[i]+i+1 for i in range(N)]
R = [i + 1 - A[i] for i in range(N)]
from collections import defaultdict
l = defaultdict(int)
for i in L:
  l[i] += 1
ans = 0
for r in R:
  ans += l.get(r, 0)
print(ans)

Solution (Python):
import collections

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







i = [k for k in range(1,N+1)]
j = [k for k in range(1,N+1)]

Li = [i[k]+A[k] for k in range(N)]
Rj = [j[k]-A[k] for k in range(N)]

Li = collections.Counter(Li)

counter = 0
for r in Rj:
    counter += Li[r]
print(counter)

Solution (Python):
from collections import defaultdict

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

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

ans = 0

for x in B.keys():
  ans += B[x] * C[x]

print(ans)

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


pp =[]
pm = []
all_set = set()
for i in range(N):

    pp.append(i + A[i])
    pm.append( i - A[i])

count = 0
c = collections.Counter(pp)

for i in range(N):
    count += c[pm[i]] 
    

    
    

print(count)

Solution (Python):
import collections
n = int(input())
a_list = list(map(int,input().split()))
a_list2 = [i-a for i,a in enumerate(a_list)]
a_list_dic = collections.Counter(a_list2)
count = 0
for i,a in enumerate(a_list):
    a_list_dic[i-a] -= 1
    count += a_list_dic[i+a]

print(count)

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

cnt={}
ans = 0
for i in range(1,n+1):
    if a[i] +i not in cnt:
        cnt[a[i] +i] = 0
    cnt[a[i] + i] += 1
for i in range(1, n+1):
    if i-a[i] in cnt:
        ans+= cnt[i-a[i]]
print(ans)

Solution (Python):
import collections

cnt = collections.defaultdict(int)
N = int(input())
ans = 0
height = list(map(int, input().split()))
for n in range(1, N+1):
    ans += cnt[n - height[n-1]]
    cnt[n+height[n-1]] += 1
print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
L = []
for i in range(N):
    L.append(i-A[i])
R = []
for j in range(N):
    R.append(j+A[j])
from collections import Counter
LL = Counter(L)
RR = Counter(R)
ans = 0
for k,v in RR.items():
    ans += v*LL[k]
print(ans)

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

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

cnt = defaultdict(int)
ans = 0

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

print(ans)