human
Write me a code to minimum index i such that all the elements from index i to given index are equal and the other steps required given the following:
Minimum index i such that all the elements from index i to given index are equal | Function to return the minimum required index.
Short - circuit more comparisions as found the border point.
For cases were high = low + 1 and arr [ high ] will match with arr [ pos ] but not arr [ low ] or arr [ mid ] . In such iteration the if condition will satisfy and loop will break post that low will be updated . Hence i will not point to the correct index ..
Driver code"
How can the above be solved in Python?
assistant
#Here is code to minimum index i such that all the elements from index i to given index are equal and the other steps required.
 def minIndex ( arr , pos ) :
	low = 0
	high = pos
	i = pos
	while low < high :
		mid = ( low + high ) // 2
		if arr [ mid ] != arr [ pos ] :
			low = mid + 1
		else :
			high = mid - 1
			i = mid
			if mid > 0 and arr [ mid - 1 ] != arr [ pos ] :
				break
	return low if arr [ low ] == arr [ pos ] else i
arr = [ 2 , 1 , 1 , 1 , 5 , 2 ]