Modify given array to a non | Function to check if a non - decreasing array can be obtained by rotating the original array ; Stores copy of original array ; Sort the given vector ; Traverse the array ; Rotate the array by 1 ; If array is sorted ; If it is not possible to sort the array ; Driver Code ; Given array ; Size of the array ; Function call to check if it is possible to make array non - decreasing by rotating
def rotateArray ( arr , N ) : NEW_LINE INDENT v = arr NEW_LINE v . sort ( reverse = False ) NEW_LINE for i in range ( 1 , N + 1 , 1 ) : NEW_LINE INDENT x = arr [ N - 1 ] NEW_LINE i = N - 1 NEW_LINE while ( i > 0 ) : NEW_LINE INDENT arr [ i ] = arr [ i - 1 ] NEW_LINE arr [ 0 ] = x NEW_LINE i -= 1 NEW_LINE DEDENT if ( arr == v ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " NO " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 4 , 5 , 1 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE rotateArray ( arr , N ) NEW_LINE DEDENT