human
Leftmost and rightmost indices of the maximum and the minimum element of an array | Function to return the index of the rightmost minimum element from the array.
First element is the minimum in a sorted array.
While the elements are equal to the minimum update rightMin.
Final check whether there are any elements which are equal to the minimum.
Function to return the index of the leftmost maximum element from the array.
Last element is the maximum in a sorted array.
While the elements are equal to the maximum update leftMax.
Final check whether there are any elements which are equal to the maximum.
Driver code.
First element is the leftmost minimum in a sorted array.
Last element is the rightmost maximum in a sorted array"
Solution in Python:
 Write me a code to leftmost and rightmost indices of the maximum and the minimum element of an array and the other steps required?
assistant
#Here is code to leftmost and rightmost indices of the maximum and the minimum element of an array and the other steps required.
 def getRightMin ( arr , n ) :
	min = arr [ 0 ]
	rightMin = 0
	i = 1
	while ( i < n ) :
		if ( arr [ i ] == min ) :
			rightMin = i
		i *= 2
	i = rightMin + 1
	while ( i < n and arr [ i ] == min ) :
		rightMin = i
		i += 1
	return rightMin
def getLeftMax ( arr , n ) :
	max = arr [ n - 1 ]
	leftMax = n - 1
	i = n - 2
	while ( i > 0 ) :
		if ( arr [ i ] == max ) :
			leftMax = i
		i = int ( i / 2 )
	i = leftMax - 1
	while ( i >= 0 and arr [ i ] == max ) :
		leftMax = i
		i -= 1
	return leftMax
if __name__ == ' _ _ main _ _ ' :
	arr = [ 0 , 0 , 1 , 2 , 5 , 5 , 6 , 8 , 8 ]
	n = len ( arr )
	print ( " Minimum ▁ left ▁ : " , 0 )
	print ( " Minimum ▁ right ▁ : " , getRightMin ( arr , n ) )
	print ( " Maximum ▁ left ▁ : " , getLeftMax ( arr , n ) )
	print ( " Maximum ▁ right ▁ : " , ( n - 1 ) )