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()))[::-1]

import collections

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

cL = collections.Counter(L)
cR = collections.Counter(R)

cnt = 0

MAX = 200000
for x in range(MAX):
	cnt += cL[x] * cR[x]

print(cnt)

Solution (Python):



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



def int1(x):
    return int(x) - 1



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



def input_to_int_tuple():
    return tuple(map(int, input().split()))



def input_to_int_tuple_minus1():
    return tuple(map(int1, input().split()))


def main():
    n = int(input())
    a = input_to_int_tuple()

    from collections import defaultdict
    l = defaultdict(int)
    r = defaultdict(int)
    for i, h in enumerate(a):
        no = i + 1
        l[no + h] += 1
        r[no - h] += 1

    ret = 0
    for i in range(1, n + 1):
        ret += (l.get(i, 0) * r.get(i, 0))

    return ret


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

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
Wa = []
Sa = []
for i, j in enumerate(A):
    Wa.append(j + i + 1)
    Sa.append(i + 1 - j)
CWa = Counter(Wa)
CSa = Counter(Sa)
ans = 0

for i in CWa.keys():
    ans += CWa[i] * CSa[i]
print(ans)

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

X = {}
ans = 0
for i in range(n):
  L = i+A[i]
  R = i-A[i]
  
  if L not in X:
    X[L] = 0
    
  X[L]+=1
  
  if R in X:
    ans += X[R]
    
print(ans)

Solution (Python):
n = int(input())
li = list(map(int, input().split()))
iai = dict()
for i in range(n): 
	if(li[i] + i not in iai):
		iai[li[i] + i] = 1
	else:
		iai[li[i] + i] += 1
ans = 0
for j in range(n):
  if(j - li[j] in iai):
  	ans += iai[j - li[j]]
print(ans)

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

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
from collections import defaultdict
I = defaultdict(int)
J = defaultdict(int)
ans = 0

for i in range(N):
  I[A[i]+i] += 1
  J[i-A[i]] += 1
for i,j in I.items():
  ans += J[i]*j
    
print(ans)

Solution (Python):
import math
import numpy as np
x = int(input())
height = list(map(lambda x: int(x), input().split()))  
cands = [0 for i in range(len(height))]


count = 0
  
for i in range(len(height)):
  if i-height[i] >= 0:
    count += cands[i-height[i]]
      
  if i+height[i] < len(height):
    cands[i+height[i]] += 1

print(count)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

n, *a = map(int, read().split())
dict = defaultdict(int)
ans = 0
for i in range(n - 1, -1, -1):
    ans += dict[-a[i] - i]
    dict[a[i] - i] += 1
print(ans)

Solution (Python):
from collections import defaultdict

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

cnt = 0
L = defaultdict(lambda: 0)
R = defaultdict(lambda: 0)

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

for key in L:
    cnt += L[key] * R[key]


print(cnt)

Solution (Python):
import sys
from math import ceil, floor, sqrt, sin, cos, pi
from itertools import accumulate, permutations, combinations
from fractions import gcd 
from collections import deque, Counter
from operator import itemgetter
from heapq import heappop,heappush
sys.setrecursionlimit(10**7)
def lcm(x, y): return ((x * y) // gcd(x, y)) 

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

x = {}
y = {}
for i in range(n):
    t = i - a[i]
    s = i + a[i]
    if t in x:
        x[t] += 1
    else:
        x[t] = 1
    if s in y:
        y[s] += 1
    else:
        y[s] = 1
cnt = 0
for i in x.keys():
    if i in y:
        cnt += x[i]*y[i]
print(cnt)

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


def readstr():
    return input().strip()


def readint():
    return int(input())


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


def readstrs():
    return input().split()


N = readint()
A = list(readnums())

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

l_c = Counter(L)
r_c = Counter(R)

x1 = min(max(L), max(R))
x2 = max(min(L), min(R))

ans = 0
for i in range(x2, x1 + 1):
    ans += l_c[i] * r_c[i]

print(ans)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
n = int(input())
a = list(map(int, input().split()))
ans = 0
R = {}
L = {}
for i in range(n):
    r = i + a[i]
    l = max(i - a[i], 0)
    if r in R:
        R[r] += 1
    else:
        R[r] = 1
    if l in L:
        L[l] += 1
    else:
        L[l] = 1

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

print(ans)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10, gcd
from itertools import permutations, combinations, product, accumulate, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 1, 0, -1, 1, -1, -1, 1]
dy = [1, 0, -1, 0, 1, -1, 1, -1]
debugmode = True


def search(arr, num):
    l = 0
    r = len(arr) - 1
    while r - l > 1:
        c = (r + l) // 2
        if arr[c] > num:
            r = c
        elif arr[c] < num:
            l = c
        else:
            r = c
            l = c
    if arr[l] == num:
        return l
    elif arr[r] == num:
        return r
    else:
        return -1

n = INT()
a = LIST()

l = [i + 1 + a[i] for i in range(n)]
r = [i + 1 - a[i] for i in range(n)]
b = Counter(r)
c = list(set(r))
c.sort()
ans = 0
for i in l:
    if search(c, i) < 0:
        continue
    ans += b[i]

print(ans)

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

ij = defaultdict(int)
ji = defaultdict(int)

As = list(map(int, input().split()))
for j in range(n):
  ij[j+1+As[j]] += 1
  ji[j+1-As[j]] += 1
keys = set(ji.keys()) | set(ij.keys())
print(sum(ij[k] * ji[k] for k in keys if k > 1))

Solution (Python):
import collections
n,*a=map(int,open(0).read().split())
l=[i+1+a[i] for i in range(n)]
r=[i+1-a[i] for i in range(n)]
L=collections.Counter(l)
R=collections.Counter(r)
print(sum(L[i]*R[i] for i in L.keys()))

Solution (Python):
from collections import Counter

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

L = []
R = []
for i, j in enumerate(lis):
    L.append(i+j)
for i, j in enumerate(lis):
    R.append(i-j)
    
ref = dict(Counter(R))
ans = 0
for i in L:
    ans += ref.get(i, 0)
print(ans)

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

L = {}
R_val = []
for i in range(N):
    if i+A[i] not in L:
        L[i+A[i]] = 0
    L[i+A[i]]+=1
    R_val.append(i-A[i])
    
ans = 0
for one in R_val:
    if one in L:
        ans+=L[one]
print(ans)

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

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

    B = []
    C = []
    for i, a in enumerate(A, start=1):
        B.append(i + a)
        C.append(i - a)

    b = Counter(B)
    c = Counter(C)

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


if __name__ == "__main__":
    main()

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 
input=lambda:sys.stdin.readline().rstrip()
from collections import defaultdict
def resolve():
    n = int(input())
    A = list(map(int,input().split()))
    count = defaultdict(int)

    res = 0
    for i in range(n):
        res += count[i - A[i]]
        count[i + A[i]] += 1

    print(res)
resolve()

Solution (Python):
from collections import defaultdict

n = int(input())

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

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

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

Solution (Python):
import sys
from bisect import bisect_left
input = sys.stdin.readline
def main():
    N = int(input())
    ans = 0
    L = {}
    R = {}
    A = tuple(map(int,input().split()))
    for i,a in enumerate(A):
        if a+i in L:
            L[a+i] += 1
        else:
            L[a+i] = 1
        if i-a in R:
            R[i-a] += 1
        else:
            R[i-a] = 1
    for k in L:
        if k in R:
            ans += L[k]*R[k]
    print(ans)



if __name__ == "__main__":
    main()

Solution (Python):
from collections import defaultdict
n = int(input())
a = [0] + [int(x) for x in input().split()]

d1 = defaultdict(int)
d2 = defaultdict(int)

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

ans = 0

for k in d1.keys():
    ans += d1[k]*d2[k]
print(ans)

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
a2 = [i - a[i] for i in range(n)]
a3 = [a[i] + i for i in range(n)]
b2 = {}
for i in a2:
    b2.setdefault(i, 0)
    b2[i] += 1
b3 = {}
for i in a3:
    b3.setdefault(i, 0)
    b3[i] += 1
ans = 0
for i, j in b2.items():
    b3.setdefault(i, 0)
    ans += j * b3[i]
print(ans)

Solution (Python):
from collections import Counter


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

a_plus_idx = Counter([a[i] + i for i in range(n)])
a_minus_idx = Counter([i - a[i] for i in range(n)])

total = 0
for k, v in a_plus_idx.items():
    total += v * a_minus_idx[k]

print(total)

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

Solution (Python):


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

L = {}
for i, a in enumerate(A):
    k = i + a
    if k not in L:
        L[k] = 1
    else:
        L[k] += 1

count = 0
for j, a in enumerate(A):
    k = j - a
    if k in L:
        count += L[k]
print(count)

Solution (Python):
import collections

n = int(input())

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


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

L = collections.Counter(l)
R = collections.Counter(r)

c = 0

for i in L.keys():
    c += L.get(i, 0) * R.get(i, 0)

print(c)

Solution (Python):

import sys
from itertools import product
from collections import Counter



def solve(N, A):
    B = [i - A[i] for i in range(N)]
    C = [i + A[i] for i in range(N)]
    Bc = Counter(B)
    Cc = Counter(C)
    ct = 0
    for k, i in Bc.items():
        if k in Cc:
            ct += i * Cc[k]
    print(ct)
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    


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):
n = int(input())
from collections import defaultdict
a = list(map(int,input().split()))
ans = 0
dp = defaultdict(int)
for e,i in enumerate(a,1):
    if  e - i>= 0 :ans += dp[e-i]
    dp[i+e]+=1
print(ans)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
S,D=[0]*N,[0]*N

for i in range(N):
    if A[i]+i<N:S[A[i]+i] +=1
    if i-A[i]>0:D[i-A[i]] += 1

ans=0
for l in range(N):
    ans += S[l]*D[l]

print(ans)

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

from copy import deepcopy

B = deepcopy(A)

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

from collections import Counter

C = Counter(A)
D = Counter(B)

E = list(C.keys())

ans = 0
for i in E:
    ans += C[i]*D[i]

print(ans)

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

lr = [[0,0] for i in range(n)]

for i in range(n):
  l = i+a[i]
  if l < n:
    lr[l][0] += 1
  r = i-a[i]
  if r > 0:
    lr[r][1] += 1
count = 0
for i in range(n):
  count += lr[i][0] * lr[i][1]
print(count)

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

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

Solution (Python):
from collections import Counter

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

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

cL = Counter(L)
cR = Counter(R)

ans = 0
for i in cL.keys():
  ans += cL[i] * cR[i]

print(ans)

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

bdic=defaultdict(int)
cdic=defaultdict(int)
for i in range(N):
  bdic[alist[i]+i]+=1
  cdic[-alist[i]+i]+=1



answer=0
for b,bv in bdic.items():
  answer+=bv*cdic[b]
  
print(answer)

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n = ii()
A = iil()


ans = 0
d = {}
for i,item in enumerate(A):
    ans += d.get(i-item,0)
    d[i+item] = d.get(i+item,0)+1

print(ans)

Solution (Python):
from collections import defaultdict


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

L = defaultdict(int)
R = defaultdict(int)
for i, v in enumerate(A):
    L[i + v] += 1
    R[i - v] += 1
print(sum(L.get(i, 0) * R.get(i, 0) for i in range(n)))

Solution (Python):

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

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

cnt = 0
d = defaultdict(int)
for i, a in enu(A):
    l = (i+1) + a
    r = (i+1) - a
    cnt += d[r]
    d[l] += 1
print(cnt)

Solution (Python):
n = int(input())
a_l = list(map(int,input().split()))
ia = list(enumerate(a_l))

x_dict = {}
y_dict = {}
for i, a in ia:
    x = i+a
    y = i-a
    if x in x_dict:
        x_dict[x] += 1
    else:
        x_dict[x] = 1
    if y in y_dict:
        y_dict[y] += 1
    else:
        y_dict[y] = 1

count = 0
for xkey in x_dict.keys():
    if xkey in y_dict:
        count += x_dict[xkey] * y_dict[xkey]

print(count)

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

count = 0

def solve():
    from collections import Counter
    cc=Counter()
    ans=0
    for i,val in enumerate(A,1):
        if i-val>0:
            ans+=cc[i-val]
        cc[i+val]+=1
    print(ans)
    
def dfs(x, y):
    global count
    if x >= N or y >= N:
        return
    
    if (A[x]+A[y]) == y-x:
        count += 1

    if x+1 >= N and y+1 >= N:
        return
        
    if y+1 >= N:
        dfs(x+1, x+2)
    else:
        dfs(x, y+1)




solve()

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














MAX = 2*10**5

dpL = [0]*(MAX)
dpR = [0]*(MAX)
for i in range(N):
    num = A[i]
    if num > 200000:
        continue   
    if i+num < MAX:
        dpL[i+num] += 1
    if i >= num:
        dpR[i-num] += 1

for i in range(MAX):
    l = dpL[i]
    r = dpR[i]
    cnt += l*r

print(cnt)

Solution (Python):
import collections

N = int(input())
As =  list(map(int, input().split()))
Bs = [As[i]+i+1 for i in range(N)]
Cs =  collections.Counter(Bs)

rlt = 0

for j in range(1,N):
  c = As[j] - (j+1)
  rlt += Cs[-c]
  

print(rlt)

Solution (Python):

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

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

cnt = 0
d = defaultdict(int)
for i, a in enu(A):
    l = (i+1) + a
    r = (i+1) - a
    cnt += d[r]
    d[l] += 1
print(cnt)

Solution (Python):
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
    C[i]=i-A[i]

B.sort()
B.insert(0,-2000000000)
B.append(2000000000)
C.sort()
ans=0
for a in C:
    l1=0
    r1=N
    while r1-l1>1:
        m1=(l1+r1)//2
        if B[m1] < a:
            l1=m1
        else:
            r1=m1
    l2=0
    r2=N
    while r2-l2>1:
        m2=(l2+r2)//2
        if B[m2] <= a:
            l2=m2
        else:
            r2=m2
    ans += l2-l1

print(ans)

Solution (Python):

import sys
sysread = sys.stdin.readline

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


    A = [(idx, val) for idx, val in enumerate(A, 1) if val < N]

    ret = 0
    store = [0] * N
    for idx, val in A:
        if idx - val > 0:
            ret += store[idx - val]
        try:
            store[idx + val] += 1
        except:
            continue
    print(ret)

if __name__ == "__main__":
    run()

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
S = [0]*N
D = [0]*N

for i in range(N):
    if A[i]+i < N:
        S[A[i]+i] += 1
    if i - A[i] > 0:
        D[i - A[i]] += 1
ans = 0
for l in range(N):
    ans += S[l]*D[l]
    

print(ans)

Solution (Python):
import sys
from collections import defaultdict


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


def main():
    N = int(input())
    A = list(map(int, input().split()))
    L = defaultdict(list)
    R = defaultdict(list)
    for i in range(N):
        L[A[i] + i].append(i)
        R[i - A[i]].append(i)
    K = list(L.keys())

    answer = 0
    for k in K:
        l = len(L[k])
        r = len(R[k])
        answer += l * r
    print(answer)


if __name__ == "__main__":
    main()

Solution (Python):


from collections import Counter
n = int(input())
a = list(map(int, input().split()))
aplusi = [item+i for i,item in enumerate(a)]
iminusa = [i-item for i,item in enumerate(a)]
c1 = Counter(aplusi)
c2 = Counter(iminusa)
out = 0
for k,v in c1.items():
    if k in c2:
        out += v * c2[k]
print(out)

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

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

a = [0] + a

from collections import defaultdict

c = defaultdict(int)

ret = 0
for i in range(1, n + 1):
    c[i + a[i]] += 1

    max_diff = i - a[i]
    ret += c[max_diff]

print(ret)

Solution (Python):

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







ip=[0]*n
jm=dict()
for i in range(n):
    ip[i]=i+a[i]
    if i-a[i] in jm:
        jm[i-a[i]]+=1
    else:
        jm[i-a[i]]=1

icnt=0
for i in range(n):
    if ip[i] in jm:
        icnt+=jm[ip[i]]
            
print(icnt)

Solution (Python):
from collections import defaultdict
d_i = defaultdict(list)
d_j = defaultdict(list)

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

for i in range(N):
    d_i[i-A[i]].append(i)
    d_j[i+A[i]].append(i)

cnt = 0
for key in d_j.keys():
    arr_i = d_i[key]
    arr_j = d_j[key]
    cnt += len(arr_j) * len(arr_i)
print(cnt)

Solution (Python):
from collections import defaultdict


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

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

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):


import sys
stdin = sys.stdin
from collections import defaultdict

import numpy as np

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    a = np.array(na())
    b = np.arange(1, n+1)
    c = a + b
    d = b - a
    l = defaultdict(lambda: 0)
    ans = 0
    for ci in c:
        l[ci] += 1
    for di in d:
        ans += l[di]
    print(ans)


if __name__ == "__main__":
    main()

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

Ai = dict()
ans = 0
for i, v in enumerate(A):
    ai = i + v
    if ai in Ai:
        Ai[ai].append(i)
    else:
        Ai[ai] = [i]
for i, v in enumerate(A):
    aj = i - v
    if aj in Ai:
        ans += len(Ai[aj])
print(ans)

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


ans = 0
t = collections.defaultdict(int)
t2 = collections.defaultdict(int)
for i, x in enumerate(A):
    t[i-x] += 1
    t2[x+i] += 1
for i, x in enumerate(A):
    ans += t[x+i]
    ans += t2[i-x]

print(ans // 2)

Solution (Python):
from collections import defaultdict

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

kouho = defaultdict(int)
ans = 0
for i in range(n):
    kouho[A[i] + (i + 1)] += 1
    ans += kouho[(i + 1) - A[i]]

print(ans)

Solution (Python):
def main():
    from collections import Counter
    n = int(input())
    As = list(map(int,input().split(" ")))
    
    cnts = Counter()
    dp = [0]*n
    cnts[As[0]+1]+=1
    for i in range(1,n):
        dp[i] =dp[i-1] + cnts.get(i+1-As[i],0)
        cnts[As[i]+i+1]+=1
    print(dp[n-1])
main()

Solution (Python):
from collections import Counter

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

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

for i in range(1, N+1):
    diff = i - alist[i]
    if diff in sum_cnt:
        ans += sum_cnt[diff]
print(ans)

Solution (Python):
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
    C[i]=i-A[i]

B.sort()
B.insert(0,-2000000000)
B.append(2000000000)
C.sort()
ans=0
for a in C:
    l1=0
    r1=N
    while r1-l1>1:
        m1=(l1+r1)//2
        if B[m1] < a:
            l1=m1
        else:
            r1=m1
    l2=0
    r2=N
    while r2-l2>1:
        m2=(l2+r2)//2
        if B[m2] <= a:
            l2=m2
        else:
            r2=m2
    if l2==0 and l1 == -1:
        pass
    else:
        ans += l2-l1

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
su = dict()
sd = dict()
for i in range(n):
    if a[i] + i + 1 in su:
        su[a[i] + i + 1] += 1
    else:
        su[a[i] + i + 1] = 1
    if a[i] - i - 1 in sd:
        sd[a[i] - i - 1] += 1
    else:
        sd[a[i] - i - 1] = 1
fsu = []
for d in su:
    fsu.append([d, su[d]])
fsu.sort()
fsd = []
for d in sd:
    fsd.append([d, sd[d]])
fsd.sort()
s = 0
i1 = len(fsu) - 1
j1 = 0


while (i1 >= 0 and j1 <= len(fsd)-1):
    if (fsu[i1][0] + fsd[j1][0] == 0):
                s+=fsu[i1][1] * fsd[j1][1]
                i1-=1
    elif (fsu[i1][0] + fsd[j1][0] > 0):
                i1-=1
    elif (fsu[i1][0] + fsd[j1][0] < 0):
                j1+=1

print(s)

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

tmp1 = []
tmp2 = []
from collections import defaultdict
d1 = defaultdict(int)
d2 = defaultdict(int)
for i in range(N):
    tmp1 = A[i] + i
    tmp2 = i - A[i]
    d1[tmp1] += 1
    d2[tmp2] += 1

ans = 0

for key1, value1 in d1.items():
    if d2[key1]:
        ans += value1 * d2[key1]
print(ans)

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

ans = 0
b = {}
for i in range(n):
    l = a[i] + (i+1)
    r = (i+1) - a[i]
    if r in b:
        ans += b[r]
    if l in b:
        b[l] += 1
    else:
        b[l] = 1

print(ans)

Solution (Python):
def main():
    import sys
    from collections import Counter
    N=int(sys.stdin.readline())
    A=list(map(int,sys.stdin.readline().split()))
    C=Counter([i+x for i,x in enumerate(A)])
    output=0
    for key,val in Counter([i-x for i,x in enumerate(A)]).items():
        if key in C.keys():
            output+=val*C[key]
    print(output)

main()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=[0]*n
c=[0]*n
d=0
sums=0
for j in range(n):
    if j-a[j]<1:
        das=0
    else:
        b[j-a[j]]=b[j-a[j]]+1
    if j+a[j]>n-2:
        das=0
    else:
        c[j+a[j]]=c[j+a[j]]+1
for k in range(n):
    d=b[k]*c[k]
    sums=sums+d
print(sums)

Solution (Python):

import numpy as np

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


x = np.array(a[1:])-a[0]+np.arange(1, n)


y = np.array(a[1:])+a[0]-np.arange(0, n-1)


count = {}
ans = 0
for i in range(0, n-1):
    if not x[i] in count:
        count[x[i]] = 1
    else:
        count[x[i]] += 1

    if y[i] == 1:
        ans += 1
    if -y[i]+1 in count:
        ans += count[-y[i]+1]

print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
d = [0]*(10**6)
ans = 0
for i in range(N):
    m = i+1+A[i]
    if m < 10**6:
        d[i+1+A[i]] += 1
    
for i in range(1,N+1):
    f = A[-i]+N+1-i-(2*A[-i])
    if f >= 0 and f < 10**6:
        ans += d[f]
print(ans)

Solution (Python):
from collections import Counter

N, *X = map(int, open(0).read().split())

x = Counter([i + 1 + X[i] for i in range(N)])
y = Counter([i + 1 - X[i] for i in range(N)])

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

Solution (Python):
import sys
sys.setrecursionlimit(300000)
from collections import defaultdict

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


N = I()
A = LMI()

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

ans = 0
for i, a in enumerate(A):
    c = d[a + i + 1]
    if c > 0:
        ans += c
print(ans)

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

    B = [a+i for i, a in enumerate(A)]
    C = [a-i for i, a in enumerate(A)]

    
    c = collections.Counter(C)

    ans = 0
    for b in B:
        ans += c.get(-b, 0)
    print(ans)


if __name__ == '__main__':
    main()

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


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

    ans = 0
    b = {}
    for i in range(N):
        l = A[i] + (i+1)
        r = (i+1) - A[i]
        if r in b:
            ans += b[r]
        if l in b:
            b[l] += 1
        else:
            b[l] = 1

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) 
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    


MOD = 10**9 + 7
INF = 2**31  

from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  
from math import gcd




N = read_a_int()
A = read_ints()


def ret_kumiawase(X, Y):  
    ret = 0
    cnt = Counter()
    for i in range(N - 1, -1, -1):
        ret += cnt[X[i]]  
        cnt.update([Y[i]])  
    return ret



Ai = []
for i, a in enu(A):
    Ai.append(a - i)
Aj = []
for i, a in enu(A):
    Aj.append(-a - i)
ans = ret_kumiawase(Ai, Aj)



Ai = []
for i, a in enu(A):
    Ai.append(a + i)
Aj = []
for i, a in enu(A):
    Aj.append(-a + i)
ans += ret_kumiawase(Ai, Aj)
print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
di1 = {}
di2 = {}
for i, j in enumerate(a):
    if i-j in di1:
        di1[i-j] += 1
    else:
        di1[i-j] = 1
    if i+j in di2:
        di2[i+j] += 1
    else:
        di2[i+j] = 1

ans = 0
for i, j in di1.items():
    if i not in di2:
        continue
    ans += di2[i]*j
print(ans)

Solution (Python):






from collections import Counter





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

number = range(1, N+1)
numhei = [0] * N
for n in range(N):
    numhei[n] = number[n] + A[n]

c = Counter(numhei)
count = 0
for n in number:
    count += c[n - A[n-1]]

print(count)