Count rotations required to sort given array in non | Function to count minimum anti - clockwise rotations required to sort the array in non - increasing order ; Stores count of arr [ i + 1 ] > arr [ i ] ; Store last index of arr [ i + 1 ] > arr [ i ] ; Traverse the given array ; If the adjacent elements are in increasing order ; Increment count ; Update index ; Prthe result according to the following conditions ; Otherwise , it is not possible to sort the array ; Given array ; Function Call
def minMovesToSort ( arr , N ) : NEW_LINE INDENT count = 0 NEW_LINE index = 0 NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT if ( arr [ i ] < arr [ i + 1 ] ) : NEW_LINE INDENT count += 1 NEW_LINE index = i NEW_LINE DEDENT DEDENT if ( count == 0 ) : NEW_LINE INDENT print ( "0" ) NEW_LINE DEDENT elif ( count == N - 1 ) : NEW_LINE INDENT print ( N - 1 ) NEW_LINE DEDENT elif ( count == 1 and arr [ 0 ] <= arr [ N - 1 ] ) : NEW_LINE INDENT print ( index + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT arr = [ 2 , 1 , 5 , 4 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE minMovesToSort ( arr , N ) NEW_LINE