human
Count the pairs in an array such that the difference between them and their indices is equal | Function to return the count of all valid pairs.
To store the frequencies of ( arr [ i ] - i ).
To store the required count.
If cnt is the number of elements whose difference with their index is same then ( ( cnt * ( cnt - 1 ) ) / 2 ) such pairs are possible.
Driver code"
Solution in Python:
 ... Give me a code to count the pairs in an array such that the difference between them and their indices is equal and the other steps required?
assistant
#Here is code to count the pairs in an array such that the difference between them and their indices is equal and the other steps required.
 def countPairs ( arr , n ) :
	map = dict ( )
	for i in range ( n ) :
		map [ arr [ i ] - i ] = map . get ( arr [ i ] - i , 0 ) + 1
	res = 0
	for x in map :
		cnt = map [ x ]
		res += ( ( cnt * ( cnt - 1 ) ) // 2 )
	return res
arr = [ 1 , 5 , 6 , 7 , 9 ]
n = len ( arr )
print ( countPairs ( arr , n ) )