human
Count unordered pairs of equal elements for all subarrays | Function to count all pairs ( i , j ) such that arr [ i ] equals arr [ j ] in all possible subarrays of the array.
Stores the size of the array.
Stores the positions of all the distinct elements.
Append index corresponding to arr [ i ] in the map.
Traverse the map M.
Traverse the array.
Update the value of ans.
Update the value of the sum.
Print the value of ans.
Driver Code"
Solution in Python:
assistant
#Here is code to count unordered pairs of equal elements for all subarrays and the other steps required.
 def countPairs ( arr ) :
	N = len ( arr )
	ans = 0
	M = { }
	for i in range ( len ( arr ) ) :
		if arr [ i ] in M :
			M [ arr [ i ] ] . append ( i )
		else :
			M [ arr [ i ] ] = [ i ]
	for key , value in M . items ( ) :
		v = value
		sum1 = 0
		for j in range ( len ( v ) ) :
			ans += ( sum1 * ( N - v [ j ] ) )
			sum1 += v [ j ] + 1
	print ( ans )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 1 , 2 , 1 , 1 ]
	countPairs ( arr )