human
Give me a code to maximum distance between two unequal elements and the other steps required given the following:
Maximum distance between two unequal elements | Function to return the maximum distance between two unequal elements.
If first and last elements are unequal they are maximum distance apart.
Fix first element as one of the elements and start traversing from the right.
Break for the first unequal element.
To store the distance from the first element.
Fix last element as one of the elements and start traversing from the left.
Break for the first unequal element.
To store the distance from the last element.
Maximum possible distance.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to maximum distance between two unequal elements and the other steps required.
 def maxDistance ( arr , n ) :
	if ( arr [ 0 ] != arr [ n - 1 ] ) :
		return ( n - 1 ) ;
	i = n - 1 ;
	while ( i > 0 ) :
		if ( arr [ i ] != arr [ 0 ] ) :
			break ;
		i -= 1 ;
	distFirst = - 1 if ( i == 0 ) else i ;
	i = 0 ;
	while ( i < n - 1 ) :
		if ( arr [ i ] != arr [ n - 1 ] ) :
			break ;
		i += 1 ;
	distLast = - 1 if ( i == n - 1 ) else ( n - 1 - i ) ;
	maxDist = max ( distFirst , distLast ) ;
	return maxDist ;
arr = [ 4 , 4 , 1 , 2 , 1 , 4 ] ;
n = len ( arr ) ;
print ( maxDistance ( arr , n ) ) ;