Python3 Program to Count rotations required to sort given array in non-increasing order Last Updated : 23 Jul, 2025 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! Create Quiz Comment K kartik Follow 0 Improve K kartik Follow 0 Improve Article Tags : Python rotation Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like