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 bisect,sys
import math
from collections import deque

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


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


def isRaw():
    return list(map(int, ssRaw()))

INF = 1 << 29

DIV = (10**9) + 7

def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def CONV_TBL(max, mod=DIV):
    fac, finv, inv = [0]*max, [0]*max, [0]*max
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, max):
        fac[i] = fac[i-1]*i % mod
        inv[i] = mod - inv[mod % i] * (mod//i) % mod
        finv[i] = finv[i-1]*inv[i] % mod

    class CONV:
        def __init__(self):
            self.fac =fac
            self.finv = finv
            pass

        def ncr(self, n, k):
            if(n < k):
                return 0
            if(n < 0 or k < 0):
                return 0
            return fac[n]*(finv[k]*finv[n-k] % DIV) % DIV
        
    return CONV()


def cumsum(As):
    s = 0
    for a in As:
        s+=a
        yield s


sys.setrecursionlimit(200005)


def main():
    N = iRaw()
    As = isRaw()
    ais = [(Aa+a,a) for a,Aa in enumerate(As)]
    ais.sort()
    ais0 = [ai[0] for ai in ais]
    ans = 0
    for b,Ab in enumerate(As):
        num = b-As[b]
        l = bisect.bisect_left(ais0, num)
        r = bisect.bisect_right(ais0, num)
        ans+=max(r-l,0)
    return ans

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
left = [0] * (N+1)
right = [0] * (N+1)
for i, a in enumerate(A):
    i = i+1
    if i+a <= N:
        left[i+a] += 1
    if i-a >= 1:
        right[i-a] += 1
ans = 0
for l, r in zip(left, right):
    ans += l * r
print(ans)

Solution (Python):
from collections import Counter


n = int(input())
an = list(map(int, input().split(' ')))

nn = list(range(1, n + 1))

nam = [None] * n
for i in range(n):
    nam[i] = nn[i] - an[i]
    
nap = [None] * n
for i in range(n):
    nap[i] = nn[i] + an[i]
    
c = Counter()
for p in nap:
    c[p] += 1

ans = 0
for m in nam:
    ans += c[m]

print(ans)

Solution (Python):
n,*x = map(int,open(0).read().split())
ans = 0
xr = {}
xl = {}
for i in range(n):
  xrt = i+x[i]
  if xrt < n:
    if xrt in xr:
      xr[xrt] += 1
    else:
      xr[xrt] = 1
  xlt = i-x[i]
  if xlt >= 0:
    if xlt in xl:
      xl[xlt] += 1
    else:
      xl[xlt] = 1
for i in xr.keys():
  if i in xl:
    ans += xr[i]*xl[i]
print(ans)

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

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

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

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

for i in range(n):
  if i-ls[i]>=0:
    d[i-ls[i]][1]+=1
  if i+ls[i]<n:
    d[i+ls[i]][0]+=1

ans = 0
for a,b in d:
  ans += a*b
print(ans)

Solution (Python):
N = int(input())
right_count = [0 for i in range(N)]
left_count = [0 for i in range(N)]
A = list(map(int, input().split()))

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

answer = 0
for i in range(N):
    answer += left_count[i] * right_count[i]

print(answer)

Solution (Python):
import sys
import math  
import bisect  
import queue  


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


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

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

    cnt = {}
    for b in B:
        if b not in cnt:
            cnt[b] = 1
        else:
            cnt[b] += 1

    res = 0
    for i in range(N - 1, -1, -1):
        r = (i + 1) - A[i]
        if r in cnt:
            res += cnt[r]

    print(res)


if __name__ == '__main__':
    main()

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()))
    d1 = defaultdict(int)
    d2 = defaultdict(int)

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

    ans = 0
    for val in d1:
        ans += d1[val] * d2[val]
    print(ans)



if __name__ == "__main__":
    main()

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

from collections import defaultdict

cnt = defaultdict(int)

for j, a in enumerate(A, 1):
    if j == 1: continue
    cnt[j - a] += 1

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

print(ans)

Solution (Python):
from collections import Counter

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

ans = 0
counts = Counter()
for i, h in enumerate(A):
    ans += counts[i - h]
    counts[h + i] += 1
print(ans)

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

ai = []
d = {}
for i, item in enumerate(a):
    ai.append(i + item)
    aj = i - item
    if aj in d:
        d[aj].append(i)
    else:
        d[aj] = [i]
ans = 0
for i, aii in enumerate(ai):
    if aii in d:
        index = bisect.bisect_right(d[aii], i)
        ans += len(d[aii]) - index


print(ans)

Solution (Python):
import bisect
import copy
import heapq
import sys
import itertools
import math
import queue
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]
def init_dp1(init, N): return [init for _ in range(N)]
def init_dp2(init, N, M): return [[init for _ in range(M)] for _ in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n) 


def main():
    N = int(input())
    A = read_list()

    S = [A[0] + A[i] - i for i in range(1, N)]
    D = {}
    for s in S:
        D[s] = D.setdefault(s, 0) + 1

    res = 0
    t = 0
    for i in range(N - 1):
        res += D.setdefault(t, 0)
        D[S[i]] -= 1
        t -= A[i + 1] - A[i] + 1
    
    print(res)


if __name__ == "__main__":
    main()

Solution (Python):
import bisect
N=int(input())
A=list(map(int,input().split(' ')))
A_sort = sorted([A[i]-i for i in range(N)])
ans = 0
for n,i in enumerate(A):
    ans += bisect.bisect_right(A_sort,-(n+i))-bisect.bisect_left(A_sort,-(n+i))
print(ans)

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

def main():

    N = int(input())
    a = list(map(int,input().split()))
    pl = {}
    ans = 0
    for i,num in enumerate(a):
        s = num+i
        t = num-i
        if -t in pl:
            ans += pl[-t]
        if s not in pl:
            pl[s] = 1
        else:
            pl[s] += 1

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

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

B = np.array(A) - np.arange(1,N+1)
C = np.array(A) + np.arange(1,N+1)

cntB = Counter(B)
cntC = Counter(C)

ans = 0
for k,v in cntB.items():
    ans += v * cntC[-k]
print(ans)

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


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

cnt_l = {}
for item in l:
    key = str(item)
    cnt_l.setdefault(key, 0)
    cnt_l[key] += 1
    
cnt_r = {}
for item in r:
    key = str(item)
    cnt_r.setdefault(key, 0)
    cnt_r[key] += 1

ans = 0
for key, val in cnt_l.items():
    cnt_r.setdefault(key, 0)
    ans += val * cnt_r[key]

print(ans)

Solution (Python):
N = int(input())
a = list(map(int,input().split()))
s = {}
w = {}
for i in range(N):
	if i+1-a[i] < 2:
		pass
	elif i+1-a[i] in s:
		s[i+1-a[i]] += 1
	else:
		s[i+1-a[i]] = 1
	if i+1+a[i] >= N:
		pass
	elif i+1+a[i] in w:
		w[i+1+a[i]] += 1
	else:
		w[i+1+a[i]] = 1
ans = 0
for key, value in s.items():
	if key in w:
		ans += value*(w[key])
print(ans)

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

Solution (Python):
n=int(input())
A = [0]
A.extend([int(i) for i in input().split()])
d1 = {}
d2 = {}
for i in range(1, n+1):
	u = A[i] + i
	v = A[i] - i
	d1[u] = d1.get(u,0)+1
	d2[v] = d2.get(v,0)+1



	
ans = 0

for e in d1:
	ans += d1[e] * d2.get(-e,0)
	
print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 6)





















N = int(input())
A = list(map(int,input().split()))
li = [[] for _ in range(N)]
for i in range(N):
    a = A[i]
    if i-a>0:
        li[i-a].append(i)
    if i+a<N:
        li[i+a].append(i)
s = 0
for j in range(N):
    q = li[j]
    n = len(q)
    if n < 2:
        continue
    smal = 0
    big = 0
    for k in q:
        if k < j:
            smal += 1
        else:
            big += 1
    s += smal*big
print(s)

Solution (Python):
import sys
def main():
    input = sys.stdin.readline
    N=int(input())
    *A,=map(int, input().split())

    B=[i-A[i] for i in range(N)]
    C=[i+A[i] for i in range(N)]
    from collections import Counter
    b=Counter(B)
    c=Counter(C)
    ans=0
    for k,v in b.items():
        ans+=v*c[k]
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
input()
counts = {}
ans = 0
for i, h in enumerate(map(int, input().split())):
    i += 1
    ans += counts.get(i - h, 0)
    counts[i + h] = counts.get(i + h, 0) + 1
print(ans)

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

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

L1.sort()
L2.sort()

import bisect

ans = 0
for l in L1:
    a = bisect.bisect_right(L2, l)
    b = bisect.bisect_left(L2, l)
    ans += a-b

print(ans)

Solution (Python):

import sys
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return map(int,input().split())
def LI(): return list(map(int,input().split()))

def main():
    N = INT()
    A = LI()

    minus = [0]*N
    plus = [0]*N

    for i in range(N):
        minus[i] = i+1-A[i]
        plus[i] = i+1+A[i]
    from collections import defaultdict
    count = defaultdict(int)

    plus_pointer = 0
    minus_pointer = 1
    answer = 0
    while minus_pointer < N:
        count[plus[plus_pointer]] += 1
        answer += count[minus[minus_pointer]]
        plus_pointer += 1
        minus_pointer += 1
    
    print(answer)
    return
if __name__ == '__main__':
    main()

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

    API = dict()
    ans = 0
    for i,a in enumerate(A):
        api = a+i+1
        ami = i+1-a

        if ami in API:
            ans += API[ami]

        if not api in API:
            API[api]=0
        API[api]+=1
        
    print(ans)




if __name__ == "__main__":
    resolve()

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

b = [a[i] + i + 1 for i in range(n)]
memo = {}
memo[b[0]] = 1

ans = 0
for i in range(1, n):
  if 2 * i + 2 - b[i] in memo:
    ans += memo[2 * i + 2 - b[i]]
  
  if b[i] in memo:
    memo[b[i]] += 1
  else:
    memo[b[i]] = 1

print(ans)

Solution (Python):
from collections import defaultdict

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

dic = defaultdict(int)

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

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

print (ans)

Solution (Python):
import bisect

n=int(input())
alist = list(map(int, input().split()))
walist=[]
salist=[]
for i in range(n):
    walist.append(alist[i]+i+1)
    if i!=0:
        salist.append(i+1-alist[i])




salist=sorted(salist)
salist2=[]
salist3=[]
mae=0.5
for sl in salist:
    if sl==mae:
        salist2[-1][1]+=1
    else:
        salist2.append([sl, 1])
        salist3.append(sl)
    mae=sl




answer=0

for i in range(n-1):
    basho=bisect.bisect_left(salist3, walist[i])
    if basho<len(salist3):
        
        if walist[i]==salist3[basho]:
            
            answer+=salist2[basho][1]
    
    if i!=n-2:
        basho=bisect.bisect_left(salist3, salist[i])
        salist2[basho][1]-=1
    

print(answer)

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

D={}
for i in range(1,len(L)):
    if L[i] not in D:
        D[L[i]]=1
    else:
        D[L[i]]+=1

ans=0
for i in range(1,len(L)):
    if -2*i-L[i] in D:
        ans+=D[-2*i-L[i]]
print(ans)

Solution (Python):
from collections import defaultdict

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

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

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

from collections import defaultdict
lis1=defaultdict(int)
lis2=defaultdict(int)
for i in range(1,n+1):
    lis1[-a[i-1]-i] += 1
    lis2[i-a[i-1]] += 1

ans=0
for i in range(1,n+1):
    ans+=lis1[a[i-1]-i]
    ans+=lis2[a[i-1]+i]

print(ans//2)

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

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

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

x=dict()
y=dict()
for i in range(N):
    val=R[i]
    if val in x:
        x[val]+=1
    else:
        x[val]=1

ans=0
for i in range(N):
    val=L[i]
    if val in x:
        ans+=x[val]

print(ans)

Solution (Python):
N=int(input())
A = [int(i) for i in input().split()]
left = [0] * N
right = [0] * N
for i,a in enumerate(A):
    if 0 <= i-a:
        left[i - a] += 1
    if i + a < N:
        right[i + a] += 1
ans = 0
for i in range(N):
    ans += left[i] * right[i]
print(ans)

Solution (Python):
n,*a=map(int,open(0).read().split())



from collections import Counter
b=[]
c=[]
for i,q in enumerate(a):
    b.append(i+q)
    c.append(i-q)

bb=Counter(b).items()
cc=Counter(c)


ans=0
for (bi,bj) in bb:
    ans+=bj*cc[bi]
print(ans)

Solution (Python):
import sys,bisect,string,math,time,functools,random
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,ls=None,Directed=False,index=1):
    org_inp=[];g=[[] for i in range(V)]
    FromStdin=True if ls==None else False
    for i in range(E):
        if FromStdin:
            inp=LI()
            a,b,c=(inp+[1])[:3]
            org_inp.append(inp)
        else:
            index=0
            a,b,c=(ls[i]+[1])[:3]
        if index==1:a-=1;b-=1
        aa=(a,c);bb=(b,c);g[a].append(bb)
        if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'

    mp=[boundary]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)];rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)

YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase

input=lambda: sys.stdin.readline().rstrip()

class Comb:
    def __init__(self,n,mo=10**9+7):
        self.fac=[0]*(n+1)
        self.inv=[1]*(n+1)
        self.fac[0]=1
        self.fact(n)
        for i in range(1,n+1):
            self.fac[i]=i*self.fac[i-1]%mo
            self.inv[n]*=i
            self.inv[n]%=mo
        self.inv[n]=pow(self.inv[n],mo-2,mo)
        for i in range(1,n):
            self.inv[n-i]=self.inv[n-i+1]*(n-i+1)%mo
        return
    
    def fact(self,n):
        return self.fac[n]
        
    def invf(self,n):
        return self.inv[n]

    def comb(self,x,y):
        if y<0 or y>x:
            return 0
        return self.fac[x]*self.inv[x-y]*self.inv[y]%mo

class Tree:
    def __init__(self,inp_size=None,init=True):
        if init:
            self.stdin(inp_size)
        return

    def stdin(self,inp_size=None):
        if inp_size==None:
            self.size=int(input())
        else:
            self.size=inp_size
        self.edges,_=GI(self.size,self.size-1)
        return
    
    def listin(self,ls):
        self.size=len(ls)+1
        self.edges,_=GI(self.size,self.size-1,ls)
        return

    def __str__(self):
        return  str(self.edges)

    def dfs(self,x,func=lambda prv,nx,dist:prv+dist,root_v=0):
        q=deque()
        q.append(x)
        v=[-1]*self.size
        v[x]=root_v
        while q:
            c=q.pop()
            for nb,d in self.edges[c]:
                if v[nb]==-1:
                    q.append(nb)
                    v[nb]=func(v[c],nb,d)
        return v

    def EulerTour(self,x,func=lambda prv,nx,dist:prv+dist,root_v=0):
        q=deque()
        q.append((-1,x))
        v=[None]*self.size
        v[x]=root_v
        et=[]
        while q:
            cb,ce=q.pop()
            et.append(ce)
            for nb,d in self.edges[ce]:
                if v[nb]==None:
                    q.append((nb,ce))
                    q.append((ce,nb))
                    v[nb]=func(v[ce],nb,d)
        vid=[[-1,-1]for i in range(self.size)]
        for i,j in enumerate(et):
            if vid[j][0]==-1:
                vid[j][0]=i
            else:
                vid[j][1]=i
        return v,et,vid
    
    def LCA_init(self,depth,et):
        self.st=SegTree(self.size*2-1,func=min,ide=inf)
        for i,j in enumerate(et):
            self.st.update(i,j)
        self.LCA_init_stat==True
        return
    
    def LCA(self,root,x,y):
        if self.LCA_init_stat==False:
            depth,et,vid=self.EulerTour(root)
            self.LCA_init(depth,et)
        return self.st.query(x,y+1)

show_flg=False
show_flg=True

n=I()
a=LI()
m=[]
p=[]
ans=0

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

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

print(ans)

Solution (Python):
n, *a = map(int, open(0).read().split())
x = [[0, 0] for i in range(n)]
for i in range(n):
    if 0<=i-a[i]<n: x[i-a[i]][0] += 1
    if 0<=i+a[i]<n: x[i+a[i]][1] += 1
ans = 0
for y in x:
    ans += y[0] * y[1]
print(ans)

Solution (Python):

from collections import deque, defaultdict
from itertools import combinations, combinations_with_replacement
import bisect
from math import factorial, sqrt, log, ceil, floor

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

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

    cnt = 0
    for i, a in enumerate(As):
        key = i + a
        cnt += d[key]

    print(cnt)

if __name__ == '__main__':
    main()

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ans = 0
d = {}
d[1 - A[0]] = 1
base1 = 0
base2 = 1
for a in A[1:]:
    _a = a - base1
    _a_ = 1 - a - base2
    if _a in d:
        ans += d[_a]
    if _a_ in d:
        d[_a_] += 1
    else:
        d[_a_] = 1
    base1 += 1
    base2 += 1

print(ans)

Solution (Python):
import collections
def main():

    N = int(input())
    A = list(map(int, input().split()))
    d = collections.defaultdict(int)
    ans = 0
    for j in range(N):
        if A[j] - (j+1) in d:
            ans += d[A[j]-(j+1)]
        d[-(j+1)-A[j]] += 1
    return ans


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

Solution (Python):
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math


NN = 202020
MOD = 10**9+7

INF = float("inf")

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

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

print(count)

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

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

a.insert(0, 0)
c = dict()
c[1 + a[1]] = 1
cnt = 0

for x in range(2, n + 1):
    if x - a[x] in c.keys():
        cnt += c[x - a[x]]

    if x + a[x] < 0:
        continue

    if x + a[x] in c.keys():
        c[x + a[x]] += 1
    else:
        c[x + a[x]] = 1

print(cnt)

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

c = [i - v for i, v in enumerate(a)]
check = Counter(c)
ans = 0
for i, v in enumerate(a):
    ans += check[i + v]
print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

CNT = [0] * (10 ** 7)

ans = 0
for i in range(N):
    a = A[i]
    p = i - a

    if 0 <= p <= (10 ** 7):
        ans += CNT[p]

    if 0 <= i + a <= (10 ** 7):
        CNT[i + a] += 1

print(ans)

Solution (Python):
import sys
from math import log2,floor,ceil,sqrt



Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
 
def input(): return sys.stdin.readline().strip()
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)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10**9+7

n = int(ri())
a = Ri()

dic = {}
cnt = 0
for i in range(len(a)):
    if (i+1)-a[i] > 0 and (i+1)-a[i] in dic:
        cnt+=dic[(i+1)-a[i]]

    if i+1+a[i] in dic:
        dic[i+1+a[i]]+=1
    else:
        dic[i+1+a[i]] = 1
print(cnt)

Solution (Python):
import sys,math,copy,queue,itertools,bisect,collections
from heapq import *
sys.setrecursionlimit(10**7)
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
INF = 10**18
MOD = 10**9 + 7
DD = ((1,0),(-1,0),(0,1),(0,-1))

N = NI()
A = [0] + LI()

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

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

Solution (Python):
def main():
    import sys 
    input=sys.stdin.readline 
    n=int(input())
    A=list(map(int,input().split()))
    
    dp1={}
    dp2={}
    for i in range(n):
        try:dp1[i+1+A[i]]+=1
        except:
            dp1[i+1+A[i]]=1
        if i+1>A[i]:
            try:dp2[i+1-A[i]]+=1
            except:
                dp2[i+1-A[i]]=1
        
    cnt=0
    
    
    
    for i in dp1:
        try:
            cnt+=dp1[i]*dp2[i]
        except:
            cnt+=0
    print(cnt)

if __name__=='__main__':
    main()

Solution (Python):
from collections import*
n,*a=map(int,open(0).read().split())
o=[];t=[]
for i,x in enumerate(a):o+=i+x,;t+=i-x,
f=lambda l:sum(x*(x-1)//2 for x in Counter(l).values())
print(f(o+t)-f(o)-f(t))

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

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

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

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

L=[0]*(10**6)
for i in range(N):
    if i-A[i]>=0:
        L[i-A[i]]+=1
ans=0
for i in range(N):
    tall=A[i]
    if tall+i<len(L):
        ans+=L[tall+i]
print(ans)

Solution (Python):
import sys
from collections import Counter

read = sys.stdin.read
readline = sys.stdin.readline

N, *A = map(int, read().split())
p = Counter([i + idx for idx, i in enumerate(A, 1)])
m = Counter([i - idx for idx, i in enumerate(A, 1)])
answer = 0

for i, j in p.items():
    answer += j * m[-i]

print(answer)

Solution (Python):
from math import sqrt,ceil,floor
def mi():return map(int,input().split())
def li():return list(mi())
def ii():return int(input())



n=ii()
a=li()
x=[]
m={}
for i in range(1,n+1):
    x1=a[i-1]-i
    x.append(x1)
    
    if x1 not in m:
        m[x1]=1
    else:
        m[x1]+=1
        
ans=0
for i in range(n):
    x1=x[i]-2*a[i]
    
    
    if x1 in m:
        ans+=m[x1]
        
print(ans)

Solution (Python):

import sys

def solve_(n, A):
    tmp = {}
    for i, a in enumerate(A):
        v = i + a
        if not v in tmp:
            tmp[v] = 0
        tmp[v] += 1
    ret = 0
    for i, a in enumerate(A):
        t = i - a
        if t >= 0 and t in tmp:
            ret += tmp[t]
    return ret

def solve(n, A):
    ret = solve_(n, A) + solve_(n, A[::-1])
    print(ret // 2)
    return ret

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

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
d = {}

arr = list(map(int,input().split()))
ans = 0
for i in range(n):
    diff = i+1-arr[i]
    if diff in d:
        ans += d[diff]

    ss = i+1 +arr[i]
    if ss in d:
        d[ss] += 1
    else:
        d[ss] = 1

print(ans)

Solution (Python):


from collections import defaultdict


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

    p_set = defaultdict(set)
    n_set = defaultdict(set)

    for i, a in enumerate(a_list):
        p_set[a_list[i] - i].add(i)
        n_set[- a_list[i] - i].add(i)

    r = 0
    for p in p_set:
        pl = len(p_set[p])
        nl = len(n_set[p])
        r += pl * nl
    print(r)

if __name__ == '__main__':
    main()

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

Solution (Python):


import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
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)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
A = LIST()

B = [0] * N
for i in range(N):
    B[i] = A[i] - i
C = Counter(B)
offset = 0
ans = 0
for i in range(N-1):
    C[B[i]] -= 1
    ans += C[-A[i]+offset]
    offset -= 1
print(ans)

Solution (Python):

from bisect import bisect_left, bisect_right

n = int(input())
a = list(map(int, input().split()))
m = sorted(i - x for i, x in enumerate(a))
p = sorted(i + x for i, x in enumerate(a))
cs = [0] * 10 ** 6
ans = 0
for i in range(n):
    if m[i] <= 0:
        continue
    ans += bisect_right(p, m[i], 0, i) - bisect_left(p, m[i], 0, i)
print(ans)

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

print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = list(map(int,input().split()))
D_sub=defaultdict(int)
D_add=defaultdict(int)
ans_2=0
for i,a in enumerate(A,1):
    SUB=i-a
    ans_2 += D_sub[SUB]
    D_sub[i+a]+=1

print(ans_2)