Check if given permutation of 1 to N can be counted in clockwise or anticlockwise direction | Javascript 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
function check_order ( arr ) { var cnt = 0 ; for ( 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 ; } var arr = [ 2 , 3 , 4 , 5 , 1 ] ; if ( check_order ( arr ) ) document . write ( " " ) ; else document . write ( " " ) ;