Javascript Program to Check if it is possible to sort the array after rotating it Last Updated : 26 May, 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: JavaScript <script> // Javascript implementation of above approach // check if array is sorted function is_sorted(a) { let c1=0,c2=0; // if array is ascending for(let i=0;i<n-1;i++) { if(a[i]<=a[i+1]) { c1++; } } // if array is descending for(let i=1;i<n;i++) { if(a[i]<=a[i-1]) c2++; } if(c1==n||c2==n) { return true; } return false; } // Function to check if it is possible function isPossible(a,n) { // step 1 if (is_sorted(a,n)) { document.write("Possible"); } else { // break where a[i] > a[i+1] let flag = true; let 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 (let 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; } } return false; } // Driver code let arr=[3, 1, 2, 2, 3]; let n = arr.length; if(isPossible(arr, n)) document.write("Possible"); else document.write("Not Possible"); // This code is contributed by avanitrachhadiya2155 </script> 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 Javascript Program to Check if it is possible to sort the array after rotating it kartik Follow Improve Article Tags : Greedy Sorting Competitive Programming JavaScript Web Technologies DSA rotation +3 More Practice Tags : GreedySorting Similar Reads 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 8 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 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 Javascript 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 3 min read Javascript 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[ ] 2 min read Javascript 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 Javascript 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 Find the Rotation Count in Rotated Sorted array in JavaScript A Rotated Sorted Array is an array type in JavaScript that has been rotated to the left or right by some random number of positions. In this article, we will understand how to find the count of rotations performed on the original sorted array to obtain the given sorted array using JavaScript languag 5 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 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 Like