Python3 Program for Check if an array is sorted and rotated Last Updated : 05 Sep, 2024 Comments Improve Suggest changes Like Article Like Report Given an array of N distinct integers. The task is to write a program to check if this array is sorted and rotated counter-clockwise. A sorted array is not considered as sorted and rotated, i.e., there should at least one rotation.Examples: Input : arr[] = { 3, 4, 5, 1, 2 }Output : YESThe above array is sorted and rotated.Sorted array: {1, 2, 3, 4, 5}. Rotating this sorted array clockwise by 3 positions, we get: { 3, 4, 5, 1, 2}Input: arr[] = {7, 9, 11, 12, 5}Output: YESInput: arr[] = {1, 2, 3}Output: NOInput: arr[] = {3, 4, 6, 1, 2, 5}Output: NOApproach: Find the minimum element in the array.Now, if the array is sorted and then rotate all the elements before the minimum element will be in increasing order and all elements after the minimum element will also be in increasing order.Check if all elements before minimum element are in increasing order.Check if all elements after minimum element are in increasing order.Check if the last element of the array is smaller than the starting element.If all of the above three conditions satisfies then print YES otherwise print NO.Below is the implementation of the above idea: Python3 # Python3 program to check if an # array is sorted and rotated clockwise import sys # Function to check if an array is # sorted and rotated clockwise def checkIfSortRotated(arr, n): minEle = sys.maxsize maxEle = -sys.maxsize - 1 minIndex = -1 # Find the minimum element # and it's index for i in range(n): if arr[i] < minEle: minEle = arr[i] minIndex = i flag1 = 1 # Check if all elements before # minIndex are in increasing order for i in range(1, minIndex): if arr[i] < arr[i - 1]: flag1 = 0 break flag2 = 2 # Check if all elements after # minIndex are in increasing order for i in range(minIndex + 1, n): if arr[i] < arr[i - 1]: flag2 = 0 break # Check if last element of the array # is smaller than the element just # before the element at minIndex # starting element of the array # for arrays like [3,4,6,1,2,5] - not sorted circular array if (flag1 and flag2 and arr[n - 1] < arr[0]): print("YES") else: print("NO") # Driver code arr = [3, 4, 5, 1, 2] n = len(arr) # Function Call checkIfSortRotated(arr, n) # This code is contributed # by Shrikant13 OutputYESTime Complexity: O(N), where N represents the size of the given array.Auxiliary Space: O(1), no extra space is required, so it is a constant.Please refer complete article on Check if an array is sorted and rotated for more details! Comment More infoAdvertise with us Next Article Python3 Program for Check if an array is sorted and rotated kartik Follow Improve Article Tags : Python Python Programs DSA Arrays rotation +1 More Practice Tags : Arrayspython Similar Reads Python3 Program for Search an element in a sorted and rotated array An element in a sorted array can be found in O(log n) time via binary search. But suppose we rotate an ascending order sorted array at some pivot unknown to you beforehand. So for instance, 1 2 3 4 5 might become 3 4 5 1 2. Devise a way to find an element in the rotated array in O(log n) time. Examp 8 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 For Sorting An Array Of 0s, 1s and 2s Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples: Input: {0, 1, 2, 0, 1, 2} Output: {0, 0, 1, 1, 2, 2} Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1} Output: {0, 0, 0, 5 min read Python Program to Sort an array in wave form Given an unsorted array of integers, sort the array into a wave like array. An array 'arr[0..n-1]' is sorted in wave form if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= ..... Examples: Input: arr[] = {10, 5, 6, 3, 2, 20, 100, 80} Output: arr[] = {10, 5, 6, 2, 20, 3, 100, 80} OR 3 min read Python3 Program for Block swap algorithm for array rotation Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements. Rotation of the above array by 2 will make arrayAlgorithm : Initialize A = arr[0..d-1] and B = arr[d..n-1]1) Do following until size of A is equal to size of B a) If A is shorter, divide B into Bl and Br such that Br is 3 min read 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 Python3 Program for Check if a string can be obtained by rotating another string d places Given two strings str1 and str2 and an integer d, the task is to check whether str2 can be obtained by rotating str1 by d places (either to the left or to the right).Examples: Input: str1 = "abcdefg", str2 = "cdefgab", d = 2 Output: Yes Rotate str1 2 places to the left.Input: str1 = "abcdefg", str2 3 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 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 Python List Checking and Verification Programs Lists in Python are versatile, but often, we need to verify their contents, structure, or relationships with other lists. Whether we're checking for duplicates, order, existence of elements or special conditions, Python provides efficient ways to perform these checks.This article covers various list 4 min read Like