Check if Array Is Sorted and Rotated

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

30:00

Check if Array Is Sorted and Rotated
easy
Free written solution
Topics
Companies

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

Example 1:
Input: {"nums":[3,4,5,1,2]}
Output: true
Constraints:
  • 1≤nums.length≤1001 \leq \text{nums.length} \leq 100

  • 1≤nums[i]≤1001 \leq \text{nums}[i] \leq 100

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[3,4,5,1,2]}

Bend the array into a ring, so the last element points back to the first. A sorted array that was rotated has at most one place on the ring where the value drops.

3041521324drops0allowed: at most 1

nums as given (edge after each cell)

3

0

4

1

5

2

1

3

2

4

Variables
VariableValue
n5
drops0
DepthFunction Call
Stack empty
0/8