Check if an array is sorted and rotated |  ; Function to check if an array is Sorted and rotated clockwise ; Your code here 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 Starts . ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkRotatedAndSorted ( 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 ; } int main ( ) { int arr [ ] = { 4 , 5 , 1 , 2 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( checkRotatedAndSorted ( arr , n ) ) cout << " YES " << endl ; else cout << " NO " << endl ; return 0 ; }