human
What is a code to maximum distance between two occurrences of same element in array and the other steps required given the following:
Maximum distance between two occurrences of same element in array | Function to find maximum distance between equal elements.
Used to store element to first index mapping.
Traverse elements and find maximum distance between same occurrences with the help of map ..
If this is first occurrence of element , insert its index in map.
Else update max distance.
Driver Program"
Solution in Python:
assistant
#Here is code to maximum distance between two occurrences of same element in array and the other steps required.
 def maxDistance ( arr , n ) :
	mp = { }
	maxDict = 0
	for i in range ( n ) :
		if arr [ i ] not in mp . keys ( ) :
			mp [ arr [ i ] ] = i
		else :
			maxDict = max ( maxDict , i - mp [ arr [ i ] ] )
	return maxDict
if __name__ == ' _ _ main _ _ ' :
	arr = [ 3 , 2 , 1 , 2 , 1 , 4 , 5 , 8 , 6 , 7 , 4 , 2 ]
	n = len ( arr )
	print maxDistance ( arr , n )