C++ Program to Check if it is possible to sort the array after rotating it Last Updated : 03 Mar, 2022 Comments Improve Suggest changes Like Article Like Report 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 the array to the front of the array to sort it.A = {1, 2, 3, 2} since we cannot sort it in one shuffle hence it's not possible to sort the array. Examples: Input: arr[] = {1, 2, 3, 4} Output: Possible Since this array is already sorted hence no need for shuffle. Input: arr[] = {6, 8, 1, 2, 5} Output: Possible Place last three elements at the front in the same order i.e. {1, 2, 5, 6, 8} Approach: Check if the array is already sorted or not. If yes return true.Else start traversing the array elements until the current element is smaller than next element. Store that index where arr[i] > arr[i+1].Traverse from that point and check if from that index elements are in increasing order or not.If above both conditions satisfied then check if last element is smaller than or equal to the first element of given array.Print "Possible" if above three conditions satisfied else print "Not possible" if any of the above 3 conditions failed. Below is the implementation of above approach: C++ // C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // Function to check if it is possible bool isPossible(int a[], int n) { // step 1 if (is_sorted(a, a + n)) { cout << "Possible" << endl; } else { // break where a[i] > a[i+1] bool flag = true; int i; for (i = 0; i < n - 1; i++) { if (a[i] > a[i + 1]) { break; } } // break point + 1 i++; // check whether the sequence is // further increasing or not for (int k = i; k < n - 1; k++) { if (a[k] > a[k + 1]) { flag = false; break; } } // If not increasing after break point if (!flag) return false; else { // last element <= First element if (a[n - 1] <= a[0]) return true; else return false; } } } // Driver code int main() { int arr[] = { 3, 1, 2, 2, 3 }; int n = sizeof(arr) / sizeof(arr[0]); if (isPossible(arr, n)) cout << "Possible"; else cout << "Not Possible"; return 0; } Output: Possible Time Complexity: O(n) Auxiliary Space: O(1) Please refer complete article on Check if it is possible to sort the array after rotating it for more details! Comment More infoAdvertise with us Next Article C++ Program to Check if it is possible to sort the array after rotating it kartik Follow Improve Article Tags : Greedy Sorting Technical Scripter Competitive Programming C++ Programs C++ DSA rotation +4 More Practice Tags : CPPGreedySorting Similar Reads C++ 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 C++ Program to Print array after it is right rotated K times Given an Array of size N and a values K, around which we need to right rotate the array. How to quickly print the right rotated array?Examples :  Input: Array[] = {1, 3, 5, 7, 9}, K = 2. Output: 7 9 1 3 5 Explanation: After 1st rotation - {9, 1, 3, 5, 7} After 2nd rotation - {7, 9, 1, 3, 5} Input: 2 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 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 Count of rotations required to generate a sorted array Given an array arr[], the task is to find the number of rotations required to convert the given array to sorted form.Examples: Input: arr[] = {4, 5, 1, 2, 3} Output: 2 Explanation: Sorted array {1, 2, 3, 4, 5} after 2 anti-clockwise rotations. Input: arr[] = {2, 1, 2, 2, 2} Output: 1 Explanation: So 4 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 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 Sort the Elements of an Array in Ascending Order Here, we will see how to sort the elements of an array in ascending order using a C++ program. Below are the examples: Input: 3 4 5 8 1 10Output: 1 3 4 5 8 10 Input: 11 34 6 20 40 3Output: 3 6 11 20 34 40 There are 2 ways to sort an array in ascending order in C++: Brute-force Approach Using Bubble 4 min read C++ Program for Search an element in a sorted and rotated array An element in a sorted array can be found in O(log n) time via binary search. But suppose we rotate an ascending order sorted array at some pivot unknown to you beforehand. So for instance, 1 2 3 4 5 might become 3 4 5 1 2. Devise a way to find an element in the rotated array in O(log n) time. Exam 7 min read Like