Check if Array Is Sorted and Rotated

Easy

Description

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

Examples

Input:nums = [3,4,5,1,2]
Output:true
Explanation:

[1,2,3,4,5] rotated 3 positions.

Input:nums = [3, 4, 5, 1, 2]
Output:true
Explanation:

[1,2,3,4,5] rotated 3 positions gives [3,4,5,1,2].

Input:nums = [2,1,3,4]
Output:false
Explanation:

No rotation of a sorted array can produce [2,1,3,4]. A sorted version would be [1,2,3,4], but rotating it any number of positions cannot create the given array where 1 comes after 2 but before 3.

Constraints

  • 1 ≤ nums.length ≤ 100
  • 1 ≤ nums[i] ≤ 100

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!