Skip to main content

Check if Array Is Sorted and Rotated - Solution & Explanation

EasyArray11 min readAsked at: Amazon, Microsoft, Meta +7
Practice this problem

Problem Statement

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.

There may be duplicates in the original array.

Note: An array A rotated by x positions results in an array B of the same length such that A[i] == B[(i+x) % A.length], where % is the modulo operation.

 

Example 1:

Input: nums = [3,4,5,1,2]
Output: true
Explanation: [1,2,3,4,5] is the original sorted array.
You can rotate the array by x = 3 positions to begin on the the element of value 3: [3,4,5,1,2].

Example 2:

Input: nums = [2,1,3,4]
Output: false
Explanation: There is no sorted array once rotated that can make nums.

Example 3:

Input: nums = [1,2,3]
Output: true
Explanation: [1,2,3] is the original sorted array.
You can rotate the array by x = 0 positions (i.e. no rotation) to make nums.

 

Constraints:

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100

Approach Overview

Problem Overview: You are given an integer array. The task is to check whether the array was originally sorted in non-decreasing order and then rotated some number of times. A valid rotated sorted array can have at most one position where the order decreases.

Approach 1: Simulate Rotation (O(n²) time, O(1) space)

The brute force idea is to simulate every possible rotation and check whether the resulting array is sorted. For each rotation index k, construct the rotated order and verify that every element is less than or equal to the next element while iterating through the array. If any rotation produces a sorted sequence, the array satisfies the condition. This approach directly models the definition of rotation but performs a full sorted check for each shift, leading to O(n²) time complexity.

Approach 2: Count Break Points (O(n) time, O(1) space)

A rotated sorted array has a key property: there can be at most one index where nums[i] > nums[i+1]. This position is the rotation pivot. Iterate through the array and count such "break points". Because rotation wraps around, also compare the last and first elements. If the number of breaks is more than one, the array cannot be a rotated sorted array. The algorithm performs a single pass through the array, keeping a counter and checking adjacent values, which makes it efficient and interview-friendly.

The core insight is treating the array as circular. In a perfectly sorted array there are zero decreases, while in a rotated sorted array there is exactly one. This observation eliminates the need for explicitly rotating the array or rebuilding structures.

Recommended for interviews: The break-point counting approach is what interviewers expect. It shows you recognize the structural property of a rotated sorted array and can solve the problem in O(n) time with constant space. Discussing the brute-force rotation check first demonstrates understanding of the problem definition, but implementing the single-pass solution highlights stronger algorithmic thinking.

Approach 1: Count Break Points

In a rotated sorted array, there should be exactly one point where a number is greater than its subsequent number, marking the 'rotation break'. We iterate over the array to count how many such breaks exist.

The function iterates over the array and checks for any descending order transition, counting how many such transitions exist. If more than one exists, it returns false. Otherwise, it returns true.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the number of elements in the array.
Space Complexity: O(1) as it uses a constant amount of additional space.

Try this approach in the editor →

Approach 2: Simulate Rotation

This approach involves simulating the rotation of a sorted version of the input array and checking if it matches the original array. This is done by sorting the array, and then checking each possible rotation for a match.

This solution first sorts the array and then attempts to match every rotation position of the sorted array against the input array. It utilizes a separate memory allocation to perform the simulation.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n^2) due to sorting and subsequent rotation checks.
Space Complexity: O(n) as it requires additional space for the sorted array.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

TypeScript

Rust

C

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Count Break Points

Time Complexity: O(n), where n is the number of elements in the array.
Space Complexity: O(1) as it uses a constant amount of additional space.

Simulate Rotation

Time Complexity: O(n^2) due to sorting and subsequent rotation checks.
Space Complexity: O(n) as it requires additional space for the sorted array.

Default Approach

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Simulate RotationO(n²)O(1)Useful for understanding the definition of rotation or when demonstrating the brute-force baseline
Count Break PointsO(n)O(1)Best approach for interviews and production due to linear scan and constant memory

Video Solution

Check if Array Is Sorted and Rotated - Leetcode 1752 - PythonNeetCodeIO33,528 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Check if Array Is Sorted and Rotated easy or hard?
The problem is classified as Easy on LeetCode. The main challenge is recognizing that a rotated sorted array can only have one decreasing pair. Once that insight is clear, the implementation becomes a straightforward O(n) scan.
Check if Array Is Sorted and Rotated Python/Java solution
The typical implementation iterates through the array and increments a counter whenever nums[i] > nums[i+1]. After the loop, compare nums[n-1] with nums[0] for the circular condition. This logic works identically in Python, Java, C++, JavaScript, and other languages.
How to solve Check if Array Is Sorted and Rotated in O(n)?
Traverse the array and count how many times the order decreases, meaning nums[i] > nums[i+1]. Also check the circular pair nums[n-1] and nums[0]. If the number of decreases is greater than one, the array is not a rotated sorted array. Otherwise it satisfies the condition.
What is the best approach for Check if Array Is Sorted and Rotated?
The most efficient approach is counting break points in the array. Iterate once and count positions where nums[i] > nums[i+1], including the circular comparison between the last and first element. A valid rotated sorted array has at most one such drop. This solution runs in O(n) time and O(1) space.
Is Check if Array Is Sorted and Rotated asked at Google/Amazon/Meta?
Variants of rotated array checks appear in interviews at companies like Amazon, Google, and Meta, usually as warm-up array problems. They test whether you can identify structural properties in arrays and reduce the problem to a single linear pass.
What data structure is used in Check if Array Is Sorted and Rotated?
The problem primarily uses a simple array traversal. No additional data structures such as hash maps or stacks are required. The optimal solution relies on sequential comparison of neighboring elements in the array.
What is the time complexity of Check if Array Is Sorted and Rotated?
The optimal solution runs in O(n) time because it scans the array once and compares adjacent elements. Space complexity is O(1) since only a counter variable is used. A brute-force rotation simulation would take O(n²) time.

Ready to solve this problem?

Practice Check if Array Is Sorted and Rotated with our built-in code editor and test cases.

Practice on FleetCode