human
Search an element in a sorted array formed by reversing subarrays from a random index | Function to search an element in a sorted array formed by reversing subarrays from a random index.
Set the boundaries for binary search.
Apply binary search.
Initialize the middle element.
If element found.
Random point is on right side of mid.
From l to mid arr is reverse sorted.
Random point is on the left side of mid.
From mid to h arr is reverse sorted.
Return Not Found.
Driver Code.
Given Input.
Function Call"
How can the above be solved in Python?
assistant
#Here is code to search an element in a sorted array formed by reversing subarrays from a random index and the other steps required.
 def find ( arr , N , key ) :
	l = 0
	h = N - 1
	while l <= h :
		mid = ( l + h ) // 2
		if arr [ mid ] == key :
			return mid
		if arr [ l ] >= arr [ mid ] :
			if arr [ l ] >= key >= arr [ mid ] :
				h = mid - 1
			else :
				l = mid + 1
		else :
			if arr [ mid ] >= key >= arr [ h ] :
				l = mid + 1
			else :
				h = mid - 1
	return - 1
if __name__ == " _ _ main _ _ " :
	arr = [ 10 , 8 , 6 , 5 , 2 , 1 , 13 , 12 ]
	N = len ( arr )
	key = 8
	ans = find ( arr , N , key )
	print ( ans )