Python3 Program to Check if it is possible to sort the array after rotating it Last Updated : 05 Sep, 2024 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: PossiblePlace 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: Python 3 # Python 3 implementation of # above approach def is_sorted(a): all(a[i] <= a[i + 1] for i in range(len(a) - 1)) # Function to check if # it is possible def isPossible(a, n): # step 1 if (is_sorted(a)) : print("Possible") else : # break where a[i] > a[i+1] flag = True for i in range(n - 1) : if (a[i] > a[i + 1]) : break # break point + 1 i += 1 # check whether the sequence is # further increasing or not for k in range(i, n - 1) : if (a[k] > a[k + 1]) : flag = False break # If not increasing after # break point if (not flag): return False else : # last element <= First element if (a[n - 1] <= a[0]): return True else: return False # Driver code if __name__ == "__main__": arr = [ 3, 1, 2, 2, 3 ] n = len(arr) if (isPossible(arr, n)): print("Possible") else: print("Not Possible") # This code is contributed # by ChitraNayal OutputPossibleTime 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 Python3 Program to Check if it is possible to sort the array after rotating it kartik Follow Improve Article Tags : Greedy Sorting Technical Scripter Python Competitive Programming Python Programs DSA rotation +4 More Practice Tags : GreedypythonSorting Similar Reads Python3 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 Python Program to Print array after it is right rotated K times Given an Array of size N and a value 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 5Explanation:After 1st rotation - {9, 1, 3, 5, 7}After 2nd rotation - {7, 9, 1, 3, 5}Input: Array[] 2 min read Python3 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 : YESThe above arra 3 min read Python3 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 Python3 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: Sort 4 min read Python3 Program to Find Mth element after K Right Rotations of an Array Python3 # Python3 program to implement # the above approach # Function to return Mth element of # array after k right rotations def getFirstElement(a, N, K, M): # The array comes to original state # after N rotations K %= N # If K is greater or equal to M if (K >= M): # Mth element after k right 1 min read Python3 Program to Check if a string can be obtained by rotating another string 2 places Given two strings, the task is to find if a string can be obtained by rotating another string two places. Examples: Input: string1 = "amazon", string2 = "azonam" Output: Yes // rotated anti-clockwiseInput: string1 = "amazon", string2 = "onamaz" Output: Yes // rotated clockwiseAsked in: Amazon Interv 2 min read Python3 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: 2Explan 3 min read Python3 Program to Check if two numbers are bit rotations of each other or not Given two positive integers x and y, check if one integer is obtained by rotating bits of other. Input constraint: 0 < x, y < 2^32 Bit Rotation: A rotation (or circular shift) is an operation similar to shift except that the bits that fall off at one end are put back to the other end.More info 3 min read Python3 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 3 min read Like