Modify given array to a non | C ++ program for the above approach ; Function to check if a non - decreasing array can be obtained by rotating the original array ; Stores copy of original array ; Sort the given vector ; Traverse the array ; Rotate the array by 1 ; If array is sorted ; If it is not possible to sort the array ; Driver Code ; Given array ; Size of the array ; Function call to check if it is possible to make array non - decreasing by rotating
#include <bits/stdc++.h> NEW_LINE using namespace std ; void rotateArray ( vector < int > & arr , int N ) { vector < int > v = arr ; sort ( v . begin ( ) , v . end ( ) ) ; for ( int i = 1 ; i <= N ; ++ i ) { rotate ( arr . begin ( ) , arr . begin ( ) + 1 , arr . end ( ) ) ; if ( arr == v ) { cout << " YES " << endl ; return ; } } cout << " NO " << endl ; } int main ( ) { vector < int > arr = { 3 , 4 , 5 , 1 , 2 } ; int N = arr . size ( ) ; rotateArray ( arr , N ) ; }