C++ Program to Modify given array to a non-decreasing array by rotation Last Updated : 27 Jan, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an array arr[] of size N (consisting of duplicates), the task is to check if the given array can be converted to a non-decreasing array by rotating it. If it's not possible to do so, then print "No". Otherwise, print "Yes". Examples: Input: arr[] = {3, 4, 5, 1, 2}Output: YesExplanation: After 2 right rotations, the array arr[] modifies to {1, 2, 3, 4, 5} Input: arr[] = {1, 2, 4, 3}Output: No Approach: The idea is based on the fact that a maximum of N distinct arrays can be obtained by rotating the given array and check for each individual rotated array, whether it is non-decreasing or not. Follow the steps below to solve the problem: Initialize a vector, say v, and copy all the elements of the original array into it.Sort the vector v.Traverse the original array and perform the following steps:Rotate by 1 in each iteration.If the array becomes equal to vector v, print "Yes". Otherwise, print "No". Below is the implementation of the above approach: C++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to check if a // non-decreasing array can be obtained // by rotating the original array void rotateArray(vector<int>& arr, int N) { // Stores copy of original array vector<int> v = arr; // Sort the given vector sort(v.begin(), v.end()); // Traverse the array for (int i = 1; i <= N; ++i) { // Rotate the array by 1 rotate(arr.begin(), arr.begin() + 1, arr.end()); // If array is sorted if (arr == v) { cout << "YES" << endl; return; } } // If it is not possible to // sort the array cout << "NO" << endl; } // Driver Code int main() { // Given array vector<int> arr = { 3, 4, 5, 1, 2 }; // Size of the array int N = arr.size(); // Function call to check if it is possible // to make array non-decreasing by rotating rotateArray(arr, N); } OutputYES Time Complexity: O(N2)Auxiliary Space: O(N) Please refer complete article on Modify given array to a non-decreasing array by rotation for more details! Comment More infoAdvertise with us Next Article C++ Program to Modify given array to a non-decreasing array by rotation K kartik Follow Improve Article Tags : C++ rotation array-rearrange Practice Tags : CPP Similar Reads Check if it is possible to make array increasing or decreasing by rotating the array Given an array arr[] of N distinct elements, the task is to check if it is possible to make the array increasing or decreasing by rotating the array in any direction.Examples: Input: arr[] = {4, 5, 6, 2, 3} Output: Yes Array can be rotated as {2, 3, 4, 5, 6}Input: arr[] = {1, 2, 4, 3, 5} Output: No 13 min read Javascript Program to Check if it is possible to make array increasing or decreasing by rotating the array Given an array arr[] of N distinct elements, the task is to check if it is possible to make the array increasing or decreasing by rotating the array in any direction.Examples: Input: arr[] = {4, 5, 6, 2, 3} Output: Yes Array can be rotated as {2, 3, 4, 5, 6}Input: arr[] = {1, 2, 4, 3, 5} Output: No 4 min read Javascript Program to Count rotations required to sort given array in non-increasing order Given an array arr[] consisting of N integers, the task is to sort the array in non-increasing order by minimum number of anti-clockwise rotations. If it is not possible to sort the array, then print "-1". Otherwise, print the count of rotations. Examples: Input: arr[] = {2, 1, 5, 4, 3}Output: 2Expl 3 min read Count rotations required to sort given array in non-increasing order Given an array arr[] consisting of N integers, the task is to sort the array in non-increasing order by minimum number of anti-clockwise rotations. If it is not possible to sort the array, then print "-1". Otherwise, print the count of rotations. Examples: Input: arr[] = {2, 1, 5, 4, 3}Output: 2Expl 7 min read Modify array by replacing every array element with minimum possible value of arr[j] + |j - i| Given an array arr[] of size N, the task is to find a value for each index such that the value at index i is arr[j] + |j - i| where 1 ? j ? N, the task is to find the minimum value for each index from 1 to N. Example: Input: N = 5, arr[] = {1, 4, 2, 5, 3}Output: {1, 2, 2, 3, 3}Explanation: arr[0] = 11 min read Like