Check if given permutation of 1 to N can be counted in clockwise or anticlockwise direction | Java program to check clockwise or counterclockwise order in an array ; Comparing the first and last value of array ; If the Count is greater than 1 then it can 't be  represented in required order ; Driver code
class GFG { static boolean check_order ( int [ ] arr ) { int cnt = 0 ; for ( int i = 0 ; i < arr . length - 1 ; i ++ ) { if ( Math . abs ( arr [ i + 1 ] - arr [ i ] ) > 1 ) cnt ++ ; } if ( Math . abs ( arr [ 0 ] - arr [ arr . length - 1 ] ) > 1 ) cnt ++ ; if ( cnt > 1 ) return false ; return true ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 5 , 1 } ; if ( check_order ( arr ) ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }