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 = list(map(int, input().split()))

L = []
R = []
for i, A in enumerate(A_list):
    L.append(i+A)
    R.append(i-A)


import collections
r_dict = collections.Counter(R)
ans = 0
for l in L:
    ans += r_dict.get(l, 0)
print(ans)

Solution (Python):

from collections import defaultdict


def main(A, N):
    d1 = defaultdict(int)
    d2 = defaultdict(int)

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

    cnt = 0
    for x in set(list(d1.keys())+list(d2.keys())):
        cnt += d1[x]* d2[x]

    print(cnt)
    pass

if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    main(A, N)

Solution (Python):

from collections import defaultdict


def main(A, N):
    d1 = defaultdict(int)
    d2 = defaultdict(int)

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

    cnt = 0
    for x in list(d1.keys())+list(d2.keys()):
        cnt += d1[x]* d2[x]

    print(cnt//2)
    pass

if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    main(A, N)

Solution (Python):
import collections
n = int(input())
a = list(map(int, input().split(' ')))
l, r = [], []
for i in range(n):
    l.append(i + a[i])
    r.append(i - a[i])
c = collections.Counter(r)
ans = 0
for i in l:
    ans += c.get(i, 0)
print(ans)

Solution (Python):
n = int(input())
li = list(map(int,input().split()))
ma = {}
for i,l in enumerate(li):
    if i+l+1 not in ma:
        ma[i+l+1] = 1
    else:
        ma[i+l+1] += 1

ans = 0
for i,l in enumerate(li):
    j = i+1
    tmp = 0
    if j-li[i] in ma:
        tmp = ma[j-li[i]]
    if j-li[i] == li[i]+j:
        tmp -=1
    ans += tmp
print(ans)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    

    
    

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

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    

    
    

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

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    

    
    

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

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    

    
    

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

if __name__ == '__main__':
    main()

Solution (Python):
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


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

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

    DL = Counter(L)
    DR = Counter(R)

    res = 0
    for k, v in DL.items():
        if DR.get(k, 0) != 0:
            res += DR.get(k) * v
        else:
            DR[k] = v

    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):
from collections import defaultdict

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

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



ans = 0
for r in R.keys():
    if r in L.keys():
        ans += L[r] * R[r]
print(ans)

Solution (Python):


from collections import Counter

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





diff = Counter(a[j] - (j+1) for j in range(n))


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

print(count)

Solution (Python):
from collections import defaultdict

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

rem = defaultdict(int)
ans = 0

for i, x in enumerate(lis):
    ans += rem[i - x]
    rem[i + x] += 1
print(ans)

Solution (Python):
import collections

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

d = dict(collections.Counter([i + A[i] for i in range(N)]))
ans = sum([d.get(j - A[j], 0) for j in range(N)]) 

print(ans)

Solution (Python):
from collections import defaultdict
di=defaultdict(int)
dj=defaultdict(int)
N=int(input())
A=list(map(int,input().split()))
for i,a in enumerate(A):
    di[i+a]+=1
    dj[i-a]+=1
X=set(di.keys())&set(dj.keys())
ans=0
for x in X:
    ans+=di[x]*dj[x]
print(ans)

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

cond1s = {}
cond2s = {}

for i in range(N):
	c1 = i + 1 - As[i]
	c2 = i + 1 + As[i]
	if c1 in cond1s:
		cond1s[c1] += 1
	else:
		cond1s[c1] = 1
	if c2 in cond2s:
		cond2s[c2] += 1
	else:
		cond2s[c2] = 1

ans = 0
for k in cond1s.keys():
	if k in cond2s:
		ans += cond1s[k] * cond2s[k]

print(ans)

Solution (Python):




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

iList = [0] * N
jList = [0] * N
for i in range(N):
  iList[i] = A[i] + i
  jList[i] = i - A[i]
  
from collections import defaultdict
iDic = defaultdict(int)
jDic = defaultdict(int)

for i in range(N):
  iDic[iList[i]] += 1
  jDic[jList[i]] += 1
  
ans = 0
for item in iDic.items():
  ans += jDic[item[0]] * item[1]

print(ans)

Solution (Python):
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
ans = 0
l=[]
for i in range(n):
    l.append(i-a[i])
cnt = Counter(l)
for i in range(n):
    ans += cnt[i+a[i]]

print(ans)

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

Solution (Python):


from collections import Counter

N = int(input())
H = list(map(int, input().split()))
L = []
R = []

for i, j in enumerate(H):
    L.append(i+j)
    R.append(i-j)

b = Counter(R)
ans = 0

for i in L:
    ans += b.get(i,0)
print(ans)

Solution (Python):
from collections import Counter

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

dict1 = Counter(i+n for i, n in enumerate(A))
dict2 = Counter(i-n for i, n in enumerate(A))

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

Solution (Python):
from collections import defaultdict

def main():
    n = int(input())
    l = defaultdict(int)
    r = defaultdict(int)
    for i, ai in enumerate(map(int, input().split())):
        l[i - ai] += 1
        r[i + ai] += 1
    print(sum(l[i] * r[i] for i in range(1, n - 1)))

if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter
N = int(input())
A = list(enumerate(list(map(int, input().split()))))
b = Counter([x[0]+x[1]+1 for x in A])
c = Counter([x[0]-x[1]+1 for x in A])
count = 0
for x in b.items():
    count += x[1] * c[x[0]]
print(count)

Solution (Python):


import sys
from collections import defaultdict

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

sv = defaultdict(list)
hv = defaultdict(list)

for i, ai in enumerate(a):
    sv[ai + i + 1].append(i + 1)
    hv[(i + 1) - ai].append(i + 1)


ps = 0
for x in sv:
    ps += len(sv[x]) * len(hv[x])

print(ps)

Solution (Python):


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





diff = {}
for j in range(n):
    x = a[j] - (j+1)
    if x not in diff:
        diff[x] = 0
    diff[x] += 1


count = 0
for i in range(n):
    diff[a[i] - (i+1)] -= 1  
    x = -a[i] - (i+1)
    if x in diff:
        count += diff[x]

print(count)

Solution (Python):
from collections import Counter
N = int(input())
A = list(enumerate(list(map(int, input().split()))))
b = Counter([x[0]+x[1]+1 for x in A])
c = Counter([x[0]-x[1]+1 for x in A])
count = 0
for x in b.items():
    count += x[1] * c[x[0]]
print(count)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import defaultdict 
def main():
    N = int(readline())
    A = [int(i) for i in readline().split()]
    dd_a = defaultdict(int)
    dd_b = defaultdict(int)
    for i in range(N):
        dd_a[i-A[i]] += 1
        dd_b[A[i]+i] += 1
    ans = 0
    for i in dd_a.keys():
        ans += dd_a[i] * dd_b[i]
    print(ans)
if __name__ == '__main__':
    main()

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

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

print(ans)

Solution (Python):
import collections
 
input()
p = list(map(int, input().split()))
map1 = collections.defaultdict(lambda:0)
map2 = collections.defaultdict(lambda:0)
 
for i, v in enumerate(p):
  map1[i+v] += 1
  map2[i-v] += 1

ans = 0
for i in map1:
  if i in map2:
    ans += map1[i] * map2[i]

print(ans)

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

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

ans = 0
for i in range(1, N+1):
    a = A[i-1]+i
    if a in data:
       ans += data[a]

print(ans)

Solution (Python):
n = int(input())
L_dict, R_dict = {}, {}
i = 0
for ipt in input().split():
    try:
        L_dict[i + int(ipt)] += 1
    except KeyError:
        L_dict[i + int(ipt)] = 1
    try:
        R_dict[i - int(ipt)] += 1
    except KeyError:
        R_dict[i - int(ipt)] = 1
    i += 1
cnt = 0
for Lk, Lv in L_dict.items():
    try:
        cnt += R_dict[Lk] * Lv
    except KeyError:
        pass
print(cnt)

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

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

print(ans)

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

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

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

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

print(counter)

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

dicplus = {}
dicmins = {}
for i in range(n):
	b = i + a[i]
	c = i - a[i]
	if b not in dicplus:
		dicplus[b] = 1
	else:
		dicplus[b] += 1
	if c not in dicmins:
		dicmins[c] = 1
	else:
		dicmins[c] += 1

ans = 0
for i in dicplus:
	if i in dicmins:
		ans += dicplus[i] * dicmins[i]
print (ans)

Solution (Python):
s=i=0
d={}
for a in[*open(0)][1].split():a=int(a);s+=d.get(i-a,0);d[i+a]=d.get(i+a,0)+1;i+=1
print(s)

Solution (Python):
s=i=0
d={}
for a in[*open(0)][1].split():a=int(a);s+=d.get(i-a,0);d[i+a]=d.get(i+a,0)+1;i+=1
print(s)

Solution (Python):
from collections import defaultdict

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

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

print(ans)

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

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
idx = [i+1 for i in range(n)]
B = [i-j for i,j in zip(A, idx)]
C = [-(i+j) for i,j in zip(A, idx)]
looked = {}
ans = 0
for b,c in zip(B, C):
    if b in looked.keys():
        ans += looked[b]
    if c in looked.keys():
        looked[c] += 1
    else:
        looked[c] = 1
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
INF = 10**10
MOD = 10**9 + 7
N = int(input())
A = list(map(int,input().split()))

minus = []
plus = []
for i in range(len(A)):
    minus.append(A[i]-(i+1))
    plus.append(A[i]+(i+1))

d = {}
for m in minus:
    d[m] = d.get(m,0) + 1

ans = 0
for p in plus:
    ans += d.get(-p,0)
print (ans)

Solution (Python):
from collections import*
n,*a=map(int,open(0).read().split())
s=0
d=defaultdict(int)
for i,a in enumerate(a):
  s+=d[i-a]
  d[i+a]+=1
print(s)

Solution (Python):
n,*a=map(int,open(0).read().split())
s,*d=[0]*-~n
for i,a in enumerate(a):
  if i>=a:s+=d[i-a]
  if i+a<n:d[i+a]+=1
print(s)

Solution (Python):

from collections import Counter

N = int(input())
H = [0]
H.extend(list(map(int, input().split())))

plus = [H[i] + i for i in range(1,N+1)]
minus = [i - H[i] for i in range(1, N+1)]
ans = 0







cnt = Counter(minus)

for i in plus:
    ans += cnt.get(i, 0)

print(ans)

Solution (Python):







def main():
    from collections import defaultdict as dd
    d = dd(lambda : 0)
    n = int(input())
    a = list(map(int , input().split()))

    for idx , i in enumerate(a):
        d[idx + 1 - i] += 1
    
    cnt = 0
    for idx , i in enumerate(a):
        cnt += d[i + idx + 1]
    
    print(cnt)
    return

if __name__ == "__main__":
    main()

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

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

dl = dict()
dr = dict()
for l in L:
    dl[l] = dl.get(l, 0) + 1
for r in R:
    dr[r] = dr.get(r, 0) + 1

cnt = 0
for k in dl.keys():
    cnt += dl.get(k, 0) * dr.get(k, 0)

print(cnt)

Solution (Python):
import math
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def pop2(li):
	c=1
	i=li.pop(0)
	while 1:
		j=li.pop(0)
		if j==i:
			c+=1
		else:
			break
	return i,c,[j]+li

n = readInt()
a = readInts()

li = []
ri = []

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

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

ans = 0

for key in d1:
	ans+=d1[key]*d2[key]

print(ans)

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

l = {}
r = {}

for i in range(n):
    if i+a[i] in l.keys():
        l[i+a[i]] += 1
    else:
        l[i+a[i]] = 1
    if i-a[i] in r.keys():
        r[i-a[i]] += 1
    else:
        r[i-a[i]] = 1         

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

print(ans)

Solution (Python):



import sys

def main():
    N = int(input())
    A = list(map(int, input().split()))
    dic_Lg = {}
    dic_Lw = {}
    for i in range(N):
        x = i - A[i]
        if x in dic_Lg:
            dic_Lg[x]+=1
        else:
            dic_Lg[x]=1
        y = i + A[i]
        if y in dic_Lw:
            dic_Lw[y]+=1
        else:
            dic_Lw[y]=1
    LP = list(dic_Lw.keys()) + list(dic_Lg.keys())
    LP = set(LP)
    ret = 0
    for i in LP:
        if i in dic_Lg and i in dic_Lw:
            ret += dic_Lg[i] * dic_Lw[i]
    print(ret)

if __name__ == '__main__':
    main()

Solution (Python):

import sys
from collections import Counter
from itertools import permutations, combinations

from math import gcd
from math import ceil, floor
import bisect
sys.setrecursionlimit(10 ** 6)
inf = float("inf")

def input():
    return sys.stdin.readline()[:-1]

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

if __name__ == '__main__':
    main()

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

Solution (Python):



import sys

from bisect import bisect_left, bisect_right

def main():
    N = int(input())
    A = list(map(int, input().split()))
    dic_Lg = {}
    for i in range(N):
        x = i - A[i]
        if x in dic_Lg:
            dic_Lg[x].append(i)
        else:
            dic_Lg[x]=[i]
    ret = 0
    for i in range(N):
        y = i+A[i]
        if not y in dic_Lg:
            continue
        arr = dic_Lg[y]
        r = bisect_right(arr, i)    
        ret += len(arr)-r
    print(ret)

if __name__ == '__main__':
    main()

Solution (Python):
def is_ok(mid: int, target: int, ls: []):
    return target <= ls[mid]


def bi_search(target: int, ls: []):
    ng = -1
    ok = len(ls)

    cnt = 0
    while ok - ng > 1:
        mid = ng + int((ok - ng) / 2)
        cnt += 1
        if is_ok(mid, target, ls):
            ok = mid
        else:
            ng = mid
    return ok


def main():
    n = int(input())
    l_dic = {}
    r_dic = {}
    i = 0
    for a in map(int, input().split()):
        ld = l_dic.setdefault(i + a, [])
        ld.append(i)
        rd = r_dic.setdefault(i - a, [])
        rd.append(i)
        i += 1

    count = 0
    for rk, rv_list in r_dic.items():
        if rk not in l_dic:
            continue
        lv_list = l_dic[rk]
        for rv in rv_list:
            ind = bi_search(rv, lv_list)
            if ind == len(lv_list):
                count += ind
            else:
                count += ind + 1
            
            
            
            
            

    print(count)


main()

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


def rle(list):
    dict = {}
    for i in list:
        if i in dict.keys():
            dict[i] += 1
        else:
            dict[i] = 1
    return dict


n = int(input())
a = list(map(int, input().split()))
minus = rle([i-a[i] for i in range(n)])
plus = rle([i+a[i] for i in range(n)])

cnt = 0
for i in minus:
    if i in plus:
        cnt += minus[i] * plus[i]
print(cnt)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
INF = 10**10
MOD = 10**9 + 7
N = int(input())
A = list(map(int,input().split()))

minus = []
plus = []
for i in range(len(A)):
    minus.append(A[i]-(i+1))
    plus.append(-A[i]-(i+1))

d = {}
for m in minus:
    d[m] = d.get(m,0) + 1

ans = 0
for p in plus:
    ans += d.get(p,0)
print (ans)

Solution (Python):
from collections import*
n,*a=map(int,open(0).read().split())
b,c,z=[],[],0
for i,q in enumerate(a):b+=i+q,;c+=i-q,
c=Counter(c)
for j,p in Counter(b).items():z+=p*c[j]
print(z)

Solution (Python):


from collections import defaultdict

iim = lambda: map(int, input().rstrip().split())

def resolve():
    N = int(input())
    A = list(iim())

    ans = 0
    dp = [0]*N
    for i, ai in enumerate(A):
        x = i + ai
        if x < N:
            dp[x] += 1
        x = i - ai
        if x >= 0:
            ans += dp[x]

    print(ans)

if __name__ == "__main__":
    resolve()

Solution (Python):


from collections import defaultdict

iim = lambda: map(int, input().rstrip().split())

def resolve():
    N = int(input())
    A = list(iim())

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

    ans = 0
    for k, v in d1.items():
        if k in d2:
            ans += v * d2[k]

    print(ans)

if __name__ == "__main__":
    resolve()

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

N = int(readline().rstrip())
A = list(map(int, readline().split()))

d1 = {}
d2 = {}
for n in range(1, N+1):
    x1 = n + A[n-1]
    if x1 in d1:
        d1[x1] += 1
    else:
        d1[x1] = 1
    
    x2 = n - A[n-1]
    if x2 in d2:
        d2[x2] += 1
    else:
        d2[x2] = 1

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

Solution (Python):
from collections import defaultdict

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

d = defaultdict(int)
ans = 0

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

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

print(ans)

Solution (Python):
delta = {}

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

for i, e in enumerate(a):
    delta[i - e] = delta.get(i - e, 0) + 1

for i, e in enumerate(a):
    k += delta.get(e + i, 0)

print(k)

Solution (Python):
from collections import defaultdict

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

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

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input())
a = [int(i) for i in input().split()]
from collections import Counter
c = Counter([])
ans = 0
for i,ai in enumerate(a):
  S = i+1+ai
  if i > 0:
    L = i+1-ai
    if L in c:
      ans += c[L]
  c.update({S:1})
print(ans)

Solution (Python):
_,*l=map(int,open(0).read().split());d,a,i={},0,0
for h in l:d[i+h]=d.get(i+h,0)+1;a+=d.get(i-h,0);i+=1
print(a)

Solution (Python):

from collections import defaultdict


def main():
    N, *A = map(int, open(0).read().split())
    cnt = defaultdict(int)
    ans = 0
    for i, a in enumerate(A, 1):
        cnt[i + a] += 1
        ans += cnt[i - a]
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
li1=[0]*n
li2=[0]*n
for i in range(n):
    li1[i] = i + a[i]
    li2[i] = i - a[i]

li1.sort()
li2.sort()
l1=l2=0

ans = 0

while 1:
    if li1[l1] == li2[l2]:
        cnt1=cnt2=1
        while  l1 < n-1 and li1[l1] == li1[l1+1]:
            l1 += 1
            cnt1 += 1
        while  l2 < n-1 and li2[l2] == li2[l2+1]:
            l2 += 1
            cnt2 += 1
        ans += cnt1*cnt2
        l1+=1
        l2+=1
    elif li1[l1] < li2[l2]:
        l1+=1
    else:
        l2+=1
    if l1 == n or l2 == n or (l1==l2==n-1):
        break
print(ans)

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

d = [0] * n

cnt = 0
for i in range(len(l)):
    if i + l[i] + 1 < n:
        d[i + l[i]] += 1
    if i - l[i] + 1 > 0:
        cnt += d[i - l[i]]
print(cnt)

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
res = {}
count = 0
for i in range(N):
  L = i+1 + A[i]
  res[L] = res.get(L,0) + 1
for i in range(N):
  R = i+1 - A[i]
  count = res.get(R,0) + count
print(count)

Solution (Python):




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

ilis=[0]*n
jlis=[0]*n

for i in range(n):
  ilis[i]=a[i]+i
  jlis[i]=i-a[i]

from collections import defaultdict
idic=defaultdict(int)
jdic=defaultdict(int)
for i in range(n):
  idic[ilis[i]]+=1
  jdic[jlis[i]]+=1

ans=0
for item in idic.items():
  ans+=item[1] * jdic[item[0]]

print(ans)

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

r = 0
for i, a in enumerate(A):
    t = A_j.get(a + i)
    if t:
        r += t

print(r)

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

print(ans)

Solution (Python):

from collections import Counter

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

L = []
R = []
for i in range(N):
    L.append(i+1 + A[i])
    R.append(i+1 - A[i])
l = Counter(L)
r = Counter(R)
 
cnt = 0
a = []
for i in range(1,N+1):
    if i in l and i in r:
        cnt += l[i] * r[i]
        a.append(i)
print(cnt)

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

ans = 0

R = []
for i ,Ai in enumerate(A,1):
  R.append(i+Ai)
R.sort()

L = []
for j ,Aj in enumerate(A,1):
  L.append(j-Aj)
L.sort()

for i in range(len(R)):
  r = bisect.bisect_right(L,R[i])
  l = bisect.bisect_left(L,R[i])
  ans+=r-l
 
print(ans)

Solution (Python):
from collections import Counter

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



L = []
R = []
for i in range(N):
    L.append(i+1 + A[i])
    R.append(i+1 - A[i])
l = Counter(L)
r = Counter(R)
 
cnt = 0
a = []
for i in range(1,N+1):
    if i in l and i in r:
        cnt += l[i] * r[i]
        a.append(i)
print(cnt)

Solution (Python):
from collections import defaultdict
from copy import deepcopy

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


    ans = 0
    left = defaultdict(int)
    for i, h in enumerate(A):
        ans += left[i-h]
        left[i+h] += 1

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

Solution (Python):
from collections import Counter

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



L = []
R = []
for i in range(N):
    L.append(i+1 + A[i])
    R.append(i+1 - A[i])
l = Counter(L)
r = Counter(R)
 
cnt = 0
for i in range(1,N+1):
    if i in l and i in r:
        cnt += l[i] * r[i]
print(cnt)

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

wa = {}
ans = 0
for i in range(len(a)):
    temp_wa = (i + 1) + a[i]
    if temp_wa in wa.keys():
        wa[temp_wa]+=1
    else:
        wa[temp_wa] = 1

    temp_sa = (i+1) - a[i]
    if temp_sa in wa.keys():
        ans += wa[temp_sa]

print(ans)

Solution (Python):


n = int(input())
h_list = input().split()
h_list = [int(h) for h in h_list]

count = 0
result_dict = {}

for i in range(n):
    key = (i+1) + h_list[i]
    value = result_dict.get(key, 0)
    result_dict[key] = value + 1

for j in range(n):
    key = (j+1) - h_list[j]
    count += result_dict.get(key, 0)

print(count)

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

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

Solution (Python):
from collections import Counter

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



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



cnt = 0
a=[]
for i in range(1,N+1):
    if i in l and i in r:
        cnt += l[i]*r[i]
        a.append(i)
print(cnt)

Solution (Python):
from collections import defaultdict
def main():
  n, *a = map(int,open(0).read().split())
  d, s = defaultdict(int), 0
  for i, x in enumerate(a):
    d[i+x]+=1
    s+=d[i-x]
  print(s)
main()

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

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

Y = [int(i + A[i - 1]) for i in range(1, N + 1)]
Y = Counter(Y)

cnt = 0
for i in range(1, N+1):
    X = i - A[i - 1]
    if X in Y.keys():
        cnt += Y[X]
print(cnt)

Solution (Python):
import bisect

n = int(input())
a = list(map(int, input().split()))
x = [i + 1 + a[i] for i in range(n)]
y = [i + 1 - a[i] for i in range(n)]
x.sort()
ans = 0
for i in y:
    ans += (bisect.bisect_right(x, i) - bisect.bisect_left(x, i))
print(ans)

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

Solution (Python):
import sys
from collections import Counter

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

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

Solution (Python):
from collections import defaultdict 
N = int(input())
A = [int(v) for v in input().split()]
 
ans = 0
dic = defaultdict(int)
 
for i,a in enumerate(A):
    dic[i+a] += 1

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

print(ans)

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