Java Program to Modify given array to a non-decreasing array by rotation Last Updated : 27 Jan, 2022 Comments Improve Suggest changes Like Article Like Report 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 right rotations, the array arr[] modifies to {1, 2, 3, 4, 5} Input: arr[] = {1, 2, 4, 3}Output: No Approach: The idea is based on the fact that a maximum of N distinct arrays can be obtained by rotating the given array and check for each individual rotated array, whether it is non-decreasing or not. Follow the steps below to solve the problem: Initialize a vector, say v, and copy all the elements of the original array into it.Sort the vector v.Traverse the original array and perform the following steps:Rotate by 1 in each iteration.If the array becomes equal to vector v, print "Yes". Otherwise, print "No". Below is the implementation of the above approach: Java // Java program for the above approach import java.util.*; class GFG{ // Function to check if a // non-decreasing array can be obtained // by rotating the original array static void rotateArray(int[] arr, int N) { // Stores copy of original array int[] v = arr; // Sort the given vector Arrays.sort(v); // Traverse the array for (int i = 1; i <= N; ++i) { // Rotate the array by 1 int x = arr[N - 1]; i = N - 1; while(i > 0){ arr[i] = arr[i - 1]; arr[0] = x; i -= 1; } // If array is sorted if (arr == v) { System.out.print("YES"); return; } } // If it is not possible to // sort the array System.out.print("NO"); } // Driver Code public static void main(String[] args) { // Given array int[] arr = { 3, 4, 5, 1, 2 }; // Size of the array int N = arr.length; // Function call to check if it is possible // to make array non-decreasing by rotating rotateArray(arr, N); } } // This code is contributed by splevel62. OutputYES Time Complexity: O(N2)Auxiliary Space: O(N) Please refer complete article on Modify given array to a non-decreasing array by rotation for more details! Comment More infoAdvertise with us Next Article Java Program to Modify given array to a non-decreasing array by rotation K kartik Follow Improve Article Tags : Java rotation array-rearrange Practice Tags : Java Similar Reads 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 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 Javascript Program to Maximize count of corresponding same elements in given Arrays by Rotation Given two arrays arr1[] and arr2[] of N integers and array arr1[] has distinct elements. The task is to find the maximum count of corresponding same elements in the given arrays by performing cyclic left or right shift on array arr1[]. Examples: Input: arr1[] = { 6, 7, 3, 9, 5 }, arr2[] = { 7, 3, 9, 3 min read Javascript Program to Find element at given index after a number of rotations An array consisting of N integers is given. There are several Right Circular Rotations of range[L..R] that we perform. After performing these rotations, we need to find element at a given index.Examples : Input : arr[] : {1, 2, 3, 4, 5} ranges[] = { {0, 2}, {0, 3} } index : 1 Output : 3 Explanation 4 min read Javascript Program to Find Mth element after K Right Rotations of an Array Given non-negative integers K, M, and an array arr[ ] consisting of N elements, the task is to find the Mth element of the array after K right rotations. Examples: Input: arr[] = {3, 4, 5, 23}, K = 2, M = 1 Output: 5 Explanation: The array after first right rotation a1[ ] = {23, 3, 4, 5} The array a 8 min read Like