Java Program to Count rotations required to sort given array in non-increasing order Last Updated : 27 Jan, 2022 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: -1 Approach: 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: Java // Java program for the above approach import java.util.*; class GFG{ // Function to count minimum anti- // clockwise rotations required to // sort the array in non-increasing order static void minMovesToSort(int arr[], int N) { // Stores count of arr[i + 1] > arr[i] int count = 0; // Store last index of arr[i+1] > arr[i] int index = 0; // Traverse the given array for(int i = 0; i < N - 1; i++) { // If the adjacent elements are // in increasing order if (arr[i] < arr[i + 1]) { // Increment count count++; // Update index index = i; } } // Print the result according // to the following conditions if (count == 0) { System.out.print("0"); } else if (count == N - 1) { System.out.print(N - 1); } else if (count == 1 && arr[0] <= arr[N - 1]) { System.out.print(index + 1); } // Otherwise, it is not // possible to sort the array else { System.out.print("-1"); } } // Driver Code public static void main(String[] args) { // Given array int[] arr = { 2, 1, 5, 4, 2 }; int N = arr.length; // Function Call minMovesToSort(arr, N); } } // This code is contributed by susmitakundugoaldanga Output:Â 2Â 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 Java Program to Count rotations required to sort given array in non-increasing order K kartik Follow Improve Article Tags : Java Searching Sorting Java Programs DSA Arrays rotation +3 More Practice Tags : ArraysJavaSearchingSorting Similar Reads Java 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 Java Program for 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 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: {9, 6, 4, 5, 8} Output: 2 The two inversions are {9, 6, 4} a 4 min read Java Program to Sort the Elements of an Array in Ascending Order Here, we will sort the array in ascending order to arrange elements from smallest to largest, i.e., ascending order. So the easy solution is that we can use the Array.sort method. We can also sort the array using Bubble sort.1. Using Arrays.sort() MethodIn this example, we will use the Arrays.sort() 2 min read Java 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 3 min read Java 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 Like