Check if Array Is Sorted and Rotated — LeetCode 1752 Python Solution

EasyArray
Problem
#1752
Reading time
2 min

The problem

Given an array nums, return true if the array was originally sorted in non-decreasing order, then rotated some number of positions (including zero). Otherwise, return false.

Example

Input
nums = [3,4,5,1,2]
Output
true
Explanation
[1,2,3,4,5] is the original sorted array.

Python solution

Python
class Solution:
    def check(self, nums: List[int]) -> bool:
        return sum(nums[i - 1] > v for i, v in enumerate(nums)) <= 1

Complexity

MeasureComplexity
TimeO(n)
SpaceO(1) to O(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 1752. Check if Array Is Sorted and Rotated?
LeetCode 1752. Check if Array Is Sorted and Rotated is rated Easy on LeetCode.
What topics does LeetCode 1752. Check if Array Is Sorted and Rotated cover?
LeetCode 1752. Check if Array Is Sorted and Rotated is tagged Array on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview