human
Check if an array is sorted and rotated using Binary Search | Function to return the index of the pivot.
Base cases.
Check if element at ( mid - 1 ) is pivot Consider the cases like { 4 , 5 , 1 , 2 , 3 }.
Decide whether we need to go to the left half or the right half.
Function to check if a given array is sorted rotated or not.
To check if the elements to the left of the pivot are in descending or not.
To check if the elements to the right of the pivot are in ascending or not.
If any of the above if or else is true Then the array is sorted rotated.
Else the array is not sorted rotated.
Driver code"
How can the above be solved in Python?
 ... What is a code to check if an array is sorted and rotated using Binary Search and the other steps required?
assistant
#Here is code to check if an array is sorted and rotated using Binary Search and the other steps required.
 def findPivot ( arr , low , high ) :
	if ( high < low ) :
		return - 1 ;
	if ( high == low ) :
		return low ;
	mid = ( low + high ) // 2 ;
	if ( mid < high and arr [ mid + 1 ] < arr [ mid ] ) :
		return mid ;
	if ( mid > low and arr [ mid ] < arr [ mid - 1 ] ) :
		return mid - 1 ;
	if ( arr [ low ] > arr [ mid ] ) :
		return findPivot ( arr , low , mid - 1 ) ;
	else :
		return findPivot ( arr , mid + 1 , high ) ;
def isRotated ( arr , n ) :
	l = 0 ;
	r = n - 1 ;
	pivot = - 1 ;
	if ( arr [ l ] > arr [ r ] ) :
		pivot = findPivot ( arr , l , r ) ;
		temp = pivot
		if ( l < pivot ) :
			while ( pivot > l ) :
				if ( arr [ pivot ] < arr [ pivot - 1 ] ) :
					return False ;
				pivot -= 1 ;
		else :
			pivot = temp
			pivot += 1 ;
			while ( pivot < r ) :
				if ( arr [ pivot ] > arr [ pivot + 1 ] ) :
					return False ;
				pivot + + 1 ;
		return True ;
	else :
		return False ;
if __name__ == " _ _ main _ _ " :
	arr = [ 3 , 4 , 5 , 1 , 2 ] ;
	if ( isRotated ( arr , 5 ) ) :
		print ( " True " ) ;
	else :
		print ( " False " ) ;