C++ Program to Check if it is possible to make array increasing or decreasing by rotating the array Last Updated : 08 Jun, 2022 Comments Improve Suggest changes Like Article Like Report 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 Approach: There are four possibilities: If the array is already increasing then the answer is Yes.If the array is already decreasing then the answer is Yes.If the array can be made increasing, this can be possible if the given array is first increasing up to the maximum element and then decreasing.If the array can be made decreasing, this can be possible if the given array is first decreasing up to the minimum element and then increasing. If it is not possible to make the array increasing or decreasing then print No.Below is the implementation of the above approach: C++ // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true if the array // can be made increasing or decreasing // after rotating it in any direction bool isPossible(int a[], int n) { // If size of the array is less than 3 if (n <= 2) return true; int flag = 0; // Check if the array is already decreasing for (int i = 0; i < n - 2; i++) { if (!(a[i] > a[i + 1] and a[i + 1] > a[i + 2])) { flag = 1; break; } } // If the array is already decreasing if (flag == 0) return true; flag = 0; // Check if the array is already increasing for (int i = 0; i < n - 2; i++) { if (!(a[i] < a[i + 1] and a[i + 1] < a[i + 2])) { flag = 1; break; } } // If the array is already increasing if (flag == 0) return true; // Find the indices of the minimum // and the maximum value int val1 = INT_MAX, mini = -1, val2 = INT_MIN, maxi; for (int i = 0; i < n; i++) { if (a[i] < val1) { mini = i; val1 = a[i]; } if (a[i] > val2) { maxi = i; val2 = a[i]; } } flag = 1; // Check if we can make array increasing for (int i = 0; i < maxi; i++) { if (a[i] > a[i + 1]) { flag = 0; break; } } // If the array is increasing upto max index // and minimum element is right to maximum if (flag == 1 and maxi + 1 == mini) { flag = 1; // Check if array increasing again or not for (int i = mini; i < n - 1; i++) { if (a[i] > a[i + 1]) { flag = 0; break; } } if (flag == 1) return true; } flag = 1; // Check if we can make array decreasing for (int i = 0; i < mini; i++) { if (a[i] < a[i + 1]) { flag = 0; break; } } // If the array is decreasing upto min index // and minimum element is left to maximum if (flag == 1 and maxi - 1 == mini) { flag = 1; // Check if array decreasing again or not for (int i = maxi; i < n - 1; i++) { if (a[i] < a[i + 1]) { flag = 0; break; } } if (flag == 1) return true; } // If it is not possible to make the // array increasing or decreasing return false; } // Driver code int main() { int a[] = { 4, 5, 6, 2, 3 }; int n = sizeof(a) / sizeof(a[0]); if (isPossible(a, n)) cout << "Yes"; else cout << "No"; return 0; } Output: Yes Time Complexity: O(n) Auxiliary Space: O(1) Please refer complete article on Check if it is possible to make array increasing or decreasing by rotating the array for more details! Comment More infoAdvertise with us Next Article C++ Program to Check if it is possible to make array increasing or decreasing by rotating the array kartik Follow Improve Article Tags : Mathematical C++ Programs C++ DSA Arrays rotation Constructive Algorithms +3 More Practice Tags : CPPArraysMathematical Similar Reads C++ Program to Check if it is possible to sort the array after rotating it Given an array of size N, the task is to determine whether its possible to sort the array or not by just one shuffle. In one shuffle, we can shift some contiguous elements from the end of the array and place it in the front of the array.For eg: A = {2, 3, 1, 2}, we can shift {1, 2} from the end of t 3 min read C++ Program to Modify given array to a non-decreasing array by rotation 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 min read C++ Program to Check if all array elements can be converted to pronic numbers by rotating digits Given an array arr[] of size N, the task is to check if it is possible to convert all of the array elements to a pronic number by rotating the digits of array elements any number of times. Examples: Input: {321, 402, 246, 299} Output: True Explanation: arr[0] ? Right rotation once modifies arr[0] to 3 min read C++ 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 Check if Array forms an increasing-decreasing sequence or vice versa Given an array arr[] of N integers, the task is to find if the array can be divided into 2 sub-array such that the first sub-array is strictly increasing and the second sub-array is strictly decreasing or vice versa. If the given array can be divided then print "Yes" else print "No". Examples: Input 14 min read C++ Program to Find the Mth element of the Array after K left rotations Given non-negative integers K, M, and an array arr[] with N elements find the Mth element of the array after K left rotations. Examples: Input: arr[] = {3, 4, 5, 23}, K = 2, M = 1Output: 5Explanation: The array after first left rotation a1[ ] = {4, 5, 23, 3}The array after second left rotation a2[ ] 3 min read Modify given array to a non-decreasing array by rotation 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 10 min read C++ Program to Check if a string can be obtained by rotating another string d places Given two strings str1 and str2 and an integer d, the task is to check whether str2 can be obtained by rotating str1 by d places (either to the left or to the right). Examples: Input: str1 = "abcdefg", str2 = "cdefgab", d = 2 Output: Yes Rotate str1 2 places to the left. Input: str1 = "abcdefg", str 4 min read C++ Program to Print all possible rotations of a given Array Given an integer array arr[] of size N, the task is to print all possible rotations of the array.Examples: Input: arr[] = {1, 2, 3, 4} Output: {1, 2, 3, 4}, {4, 1, 2, 3}, {3, 4, 1, 2}, {2, 3, 4, 1} Explanation: Initial arr[] = {1, 2, 3, 4} After first rotation arr[] = {4, 1, 2, 3} After second rotat 3 min read C++ Program for Check if an array is sorted and rotated Given an array of N distinct integers. The task is to write a program to check if this array is sorted and rotated counter-clockwise. A sorted array is not considered as sorted and rotated, i.e., there should at least one rotation.Examples: Input : arr[] = { 3, 4, 5, 1, 2 } Output : YES The above ar 5 min read Like