Skip to content
Onsite

Check if Array Is Sorted and Rotated

EasyP3Coding

nums holds distinct integers. Return true if nums is a sorted (ascending) array that was rotated by some non-zero amount, so that it is no longer in sorted order. Return false if nums is still fully sorted (including length 1) or is not a rotation of a sorted array.

A rotation by r moves the first r elements to the end: [1,2,3,4,5] rotated by 2 is [3,4,5,1,2].

Example 1

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

Example 2

Input
nums = [1,2,3]
Output
false
Why
Already sorted, no rotation.

Example 3

Input
nums = [2,1,3]
Output
false

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • All values are distinct

Hints

Run checks the 3 visible tests, Submit all 12.