Open In App

Python3 Program for Check if an array is sorted and rotated

Last Updated : 27 May, 2022
Improve
Improve
Like Article
Like
Save
Share
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 : YES
The 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: YES

Input: arr[] = {1, 2, 3}
Output: NO

Input: arr[] = {3, 4, 6, 1, 2, 5}
Output: NO

Approach
 

  • 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


Output

YES

Time 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!



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. Example: Input : arr[] = {5, 6, 7, 8, 9, 10, 1, 2, 3};
9 min read
Python3 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 the counter variable and increase the node by node
3 min read
Circularly Sorted Array (Sorted and Rotated Array)
Circularly sorted arrays are arrays that are sorted in ascending or descending order and then rotated by a number of steps. Let us take an example to know more about circularly sorted arrays: Consider an array: arr[] = {23, 34, 45, 12, 17, 19}The elements here, {12, 17, 19, 23, 34, 45} are sorted 'In-order' but they are rotated to the left by 3 tim
7 min read
Python3 Program to Find Lexicographically smallest rotated sequence | Set 2
Write code to find lexicographic minimum in a circular array, e.g. for the array BCABDADAB, the lexicographic minimum is ABBCABDADInput Constraint: 1 Examples:   Input: GEEKSQUIZ Output: EEKSQUIZG Input: GFG Output: FGG Input : CAPABCQ Output : ABCQCAP   We have discussed a O(n2Logn) solution in Lexicographically minimum string rotation | Set 1. He
2 min read
C++ Program for Check if an array is sorted and rotated
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 : YES The above array is sorted and rotated. Sorted array: {1, 2, 3,
5 min read
Java Program for Check if an array is sorted and rotated
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 : YES The above array is sorted and rotated. Sorted array: {1, 2, 3,
3 min read
Javascript Program for Check if an array is sorted and rotated
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 : YES The above array is sorted and rotated. Sorted array: {1, 2, 3,
3 min read
Php Program for Check if an array is sorted and rotated
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 : YES The above array is sorted and rotated. Sorted array: {1, 2, 3,
3 min read
Python3 Program for Check for Majority Element in a sorted array
Question: Write a function to find if a given integer x appears more than n/2 times in a sorted array of n integers. Basically, we need to write a function say isMajority() that takes an array (arr[] ), array’s size (n) and a number to be searched (x) as parameters and returns true if x is a majority element (present more than n/2 times). Examples:
7 min read
Check if an array is sorted and rotated using Binary Search
Pre-requisite: Check if an array is sorted and rotated using Linear SearchGiven an array arr[] of N distinct integers, the task is to check if this array is sorted when rotated counter-clockwise. A sorted array is not considered sorted and rotated, i.e., there should at least one rotation. Examples: Input: arr[] = { 3, 4, 5, 1, 2 } Output: true Exp
11 min read
Article Tags :
Practice Tags :