Count of rotations required to generate a sorted array | Java Program to find the count of rotations ; Function to return the count of rotations ; Find the smallest element ; Return its index ; If array is not rotated at all ; Driver Code
public class GFG { public static int countRotation ( int [ ] arr , int n ) { for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { return i ; } } return 0 ; } public static void main ( String [ ] args ) { int [ ] arr1 = { 4 , 5 , 1 , 2 , 3 } ; System . out . println ( countRotation ( arr1 , arr1 . length ) ) ; } }