Check if an array is sorted and rotated |  ; Function to check if an array is Sorted and rotated clockwise ; Initializing two variables x , y as zero . ; Traversing array 0 to last element . n - 1 is taken as we used i + 1. ; If till now both x , y are greater then 1 means array is not sorted . If both any of x , y is zero means array is not rotated . ; Checking for last element with first . ; Checking for final result . ; If still not true then definetly false . ; Driver code ; Function Call
import java . io . * ; class GFG { static boolean checkIfSortRotated ( int arr [ ] , int n ) { int x = 0 , y = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] < arr [ i + 1 ] ) x ++ ; else y ++ ; } if ( x == 1 y == 1 ) { if ( arr [ n - 1 ] < arr [ 0 ] ) x ++ ; else y ++ ; if ( x == 1 y == 1 ) return true ; } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 1 , 2 , 3 , 4 } ; int n = arr . length ; boolean x = checkIfSortRotated ( arr , n ) ; if ( x == true ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }