Python3 Program to Count rotations required to sort given array in non-increasing order Last Updated : 05 Sep, 2024 Comments Improve Suggest changes Like Article Like Report 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: 2Explanation: Two anti-clockwise rotations are required to sort the array in decreasing order, i.e. {5, 4, 3, 2, 1}Input: arr[] = {2, 3, 1}Output: -1Approach: The idea is to traverse the given array arr[] and count the number of indices satisfying arr[i + 1] > arr[i]. Follow the steps below to solve the problem:Store the count of arr[i + 1] > arr[i] in a variable and also store the index when arr[i+1] > arr[i].If the value of count is N - 1, then the array is sorted in non-decreasing order. The required steps are exactly (N - 1).If the value of count is 0, then the array is already sorted in non-increasing order.If the value of count is 1 and arr[0] ? arr[N - 1], then the required number of rotations is equal to (index + 1), by performing shifting of all the numbers upto that index. Also, check if arr[0] ? arr[N - 1] to ensure if the sequence is non-increasing.Otherwise, it is not possible to sort the array in non-increasing order.Below is the implementation of the above approach: Python # Python program for the above approach # Function to count minimum anti- # clockwise rotations required to # sort the array in non-increasing order def minMovesToSort(arr, N) : # Stores count of arr[i + 1] > arr[i] count = 0 # Store last index of arr[i+1] > arr[i] index = 0 # Traverse the given array for i in range(N-1): # If the adjacent elements are # in increasing order if (arr[i] < arr[i + 1]) : # Increment count count += 1 # Update index index = i # Print result according # to the following conditions if (count == 0) : print("0") elif (count == N - 1) : print( N - 1) elif (count == 1 and arr[0] <= arr[N - 1]) : print(index + 1) # Otherwise, it is not # possible to sort the array else : print("-1") # Driver Code # Given array arr = [ 2, 1, 5, 4, 2 ] N = len(arr) # Function Call minMovesToSort(arr, N) # This code i contributed by sanjoy_62. Output2 Time Complexity: O(N)Auxiliary Space: O(1)Please refer complete article on Count rotations required to sort given array in non-increasing order for more details! Comment More infoAdvertise with us Next Article Python3 Program to Count rotations required to sort given array in non-increasing order kartik Follow Improve Article Tags : Searching Sorting Python Python Programs DSA Arrays rotation +3 More Practice Tags : ArrayspythonSearchingSorting Similar Reads 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 Python Program to Count Inversions of size three in a given array Given an array arr[] of size n. Three elements arr[i], arr[j] and arr[k] form an inversion of size 3 if a[i] > a[j] >a[k] and i < j < k. Find total number of inversions of size 3.Example : Input: {8, 4, 2, 1} Output: 4 The four inversions are (8,4,2), (8,4,1), (4,2,1) and (8,2,1). Input: 4 min read Python3 Program to Count rotations in sorted and rotated linked list Given a linked list of n nodes which is first sorted, then rotated by k elements. Find the value of k.The idea is to traverse singly linked list to check condition whether current node value is greater than value of next node. If the given condition is true, then break the loop. Otherwise increase t 3 min read Python3 Program for Queries to find maximum sum contiguous subarrays of given length in a rotating array Given an array arr[] of N integers and Q queries of the form {X, Y} of the following two types:If X = 1, rotate the given array to the left by Y positions.If X = 2, print the maximum sum subarray of length Y in the current state of the array.Examples: Input: N = 5, arr[] = {1, 2, 3, 4, 5}, Q = 2, Qu 4 min read Python3 Program for Minimum rotations required to get the same string Given a string, we need to find the minimum number of rotations required to get the same string.Examples:Input : s = "geeks"Output : 5Input : s = "aaaa"Output : 1Method 1: The idea is based on below post.A Program to check if strings are rotations of each other or notStep 1 : Initialize result = 0 ( 2 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 Python3 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 Python Program to Find the Maximum sum of i*arr[i] among all rotations of a given array Given an array arr[] of n integers, find the maximum that maximizes the sum of the value of i*arr[i] where i varies from 0 to n-1.Examples: Input: arr[] = {8, 3, 1, 2}Output: 29Explanation: Lets look at all the rotations,{8, 3, 1, 2} = 8*0 + 3*1 + 1*2 + 2*3 = 11{3, 1, 2, 8} = 3*0 + 1*1 + 2*2 + 8*3 = 5 min read Python3 Program to Find Range sum queries for anticlockwise rotations of Array by K indices Given an array arr consisting of N elements and Q queries of the following two types: 1 K: For this type of query, the array needs to be rotated by K indices anticlockwise from its current state.2 L R: For this query, the sum of the array elements present in the indices [L, R] needs to be calculated 4 min read Python3 Program to Count rotations divisible by 4 Given a large positive number as string, count all rotations of the given number which are divisible by 4. Examples: Input: 8Output: 1Input: 20Output: 1Rotation: 20 is divisible by 4 02 is not divisible by 4Input : 13502Output : 0No rotation is divisible by 4Input : 43292816Output : 55 rotations are 2 min read Like