Python3 Program to Modify given array to a non-decreasing array by rotation Last Updated : 05 Sep, 2024 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: NoApproach: 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: Python # Python 3 program for the above approach # Function to check if a # non-decreasing array can be obtained # by rotating the original array def rotateArray(arr, N): # Stores copy of original array v = arr # Sort the given vector v.sort(reverse = False) # Traverse the array for i in range(1, N + 1, 1): # Rotate the array by 1 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): print("YES") return # If it is not possible to # sort the array print("NO") # Driver Code if __name__ == '__main__': # Given array arr = [3, 4, 5, 1, 2] # Size of the array N = len(arr) # Function call to check if it is possible # to make array non-decreasing by rotating rotateArray(arr, N) # This code is contributed by ipg2016107. OutputYESTime Complexity: O(N2)Auxiliary Space: O(N)Method 2: Identify the index of the smallest element in the array using the index method, and then creates a rotated version of the array where the smallest element is the first element. It then checks if the rotated array is non-decreasing by traversing through it and comparing each element with the next element.First, we identify the index of the smallest element in the array using the index method.We then create a rotated version of the array where the smallest element is the first element. We do this by slicing the original array into two parts and concatenating them in reverse order.Finally, we traverse through the rotated array and check if it is non-decreasing. If we find an element that is greater than the next element, we return "NO". Otherwise, we return "YES".Implementation: Python def check_rotation(arr): n = len(arr) # Identify the index of the smallest element in the array idx = arr.index(min(arr)) # Rotate the array to make the smallest element the first element rotated_arr = arr[idx:] + arr[:idx] # Check if the rotated array is non-decreasing for i in range(n-1): if rotated_arr[i] > rotated_arr[i+1]: return "NO" return "YES" # Example usage arr = [3, 4, 5, 1, 2] result = check_rotation(arr) print(result) # Output: NO OutputYESTime complexity: O(n)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 Python3 Program to Modify given array to a non-decreasing array by rotation kartik Follow Improve Article Tags : Sorting Mathematical Python Python Programs DSA Arrays rotation array-rearrange +4 More Practice Tags : ArraysMathematicalpythonSorting Similar Reads 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 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 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, 5 min read Python Program to Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed Given an array, only rotation operation is allowed on array. We can rotate the array as many times as we want. Return the maximum possible summation of i*arr[i]. Examples :Â Â Input: arr[] = {1, 20, 2, 10} Output: 72 We can get 72 by rotating array twice. {2, 10, 1, 20} 20*3 + 1*2 + 10*1 + 2*0 = 72 I 3 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 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 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 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 all array elements can be converted to pronic numbers by rotating digits Given an array arr[] of size N, the task is to check if it is possible to convert all of the array elements to a pronic number by rotating the digits of array elements any number of times.Examples:Input: {321, 402, 246, 299} Output: True Explanation: arr[0] ? Right rotation once modifies arr[0] to 1 2 min read Python3 Program to Move all zeroes to end of array Given an array of random numbers, Push all the zero's of a given array to the end of the array. For example, if the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it should be changed to {1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}. The order of all other elements should be same. Expected time complexity i 4 min read Like