Check if an Array is Sorted and Rotated (Leetcode 1752)

Problem Link: https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/

class Solution:
    def check(self, nums: List[int]) -> bool:
        
        # For all neighbourhoods including the first and last element,
        # if a>b happens for atmost one time,
        # then the array can be rotated to make it sorted.
        # Otherwise, it is not possible.
        
        n=len(nums)
        c=0
        for i in range(n-1):
            if(nums[i]>nums[i+1]):
                c+=1
        if(nums[n-1]>nums[0]):
            c+=1
        
        if(c<=1):
            return True
        else:
            return False
        

Last updated