Check if it is possible to make array increasing or decreasing by rotating the array | Python3 implementation of the approach ; Function that returns True if the array can be made increasing or decreasing after rotating it in any direction ; If size of the array is less than 3 ; Check if the array is already decreasing ; If the array is already decreasing ; Check if the array is already increasing ; If the array is already increasing ; Find the indices of the minimum and the maximum value ; Check if we can make array increasing ; If the array is increasing upto max index and minimum element is right to maximum ; Check if array increasing again or not ; Check if we can make array decreasing ; If the array is decreasing upto min index and minimum element is left to maximum ; Check if array decreasing again or not ; If it is not possible to make the array increasing or decreasing ; Driver code
import sys NEW_LINE def isPossible ( a , n ) : NEW_LINE INDENT if ( n <= 2 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT flag = 0 ; NEW_LINE for i in range ( n - 2 ) : NEW_LINE INDENT if ( not ( a [ i ] > a [ i + 1 ] and a [ i + 1 ] > a [ i + 2 ] ) ) : NEW_LINE INDENT flag = 1 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT flag = 0 ; NEW_LINE for i in range ( n - 2 ) : NEW_LINE INDENT if ( not ( a [ i ] < a [ i + 1 ] and a [ i + 1 ] < a [ i + 2 ] ) ) : NEW_LINE INDENT flag = 1 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT val1 = sys . maxsize ; mini = - 1 ; NEW_LINE val2 = - sys . maxsize ; maxi = - 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] < val1 ) : NEW_LINE INDENT mini = i ; NEW_LINE val1 = a [ i ] ; NEW_LINE DEDENT if ( a [ i ] > val2 ) : NEW_LINE INDENT maxi = i ; NEW_LINE val2 = a [ i ] ; NEW_LINE DEDENT DEDENT flag = 1 ; NEW_LINE for i in range ( maxi ) : NEW_LINE INDENT if ( a [ i ] > a [ i + 1 ] ) : NEW_LINE INDENT flag = 0 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( flag == 1 and maxi + 1 == mini ) : NEW_LINE INDENT flag = 1 ; NEW_LINE for i in range ( mini , n - 1 ) : NEW_LINE INDENT if ( a [ i ] > a [ i + 1 ] ) : NEW_LINE INDENT flag = 0 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( flag == 1 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT flag = 1 ; NEW_LINE for i in range ( mini ) : NEW_LINE INDENT if ( a [ i ] < a [ i + 1 ] ) : NEW_LINE INDENT flag = 0 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( flag == 1 and maxi - 1 == mini ) : NEW_LINE INDENT flag = 1 ; NEW_LINE for i in range ( maxi , n - 1 ) : NEW_LINE INDENT if ( a [ i ] < a [ i + 1 ] ) : NEW_LINE INDENT flag = 0 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( flag == 1 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT return False ; NEW_LINE DEDENT a = [ 4 , 5 , 6 , 2 , 3 ] ; NEW_LINE n = len ( a ) ; NEW_LINE if ( isPossible ( a , n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT