Check if Array Is Sorted and Rotated - 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.
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 B[i] == A[(i+x) % A.length] for every valid index i.
Input & Output
Example 1 — Sorted and Rotated
$
Input:
nums = [2,1,3,4]
›
Output:
true
💡 Note:
The array [1,2,3,4] was rotated by 3 positions to get [2,1,3,4]. Original: [1,2,3,4] → Rotate right by 3 → [2,1,3,4]. Only one break point at position 0→1.
Example 2 — Not Sorted Even After Rotation
$
Input:
nums = [3,4,5,1,2]
›
Output:
true
💡 Note:
Original sorted array [1,2,3,4,5] was rotated. Only one break point where 5 > 1, so it's valid.
Example 3 — Multiple Break Points
$
Input:
nums = [2,1,3,4,5,10,6,7,8]
›
Output:
false
💡 Note:
Multiple break points: 2>1, 10>6. No rotation can make this array sorted.
Constraints
- 1 ≤ nums.length ≤ 100
- -104 ≤ nums[i] ≤ 104
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Given array that might be sorted and rotated
2
Find Break Points
Count where order decreases in circular fashion
3
Validate
Return true if at most 1 break point exists
Key Takeaway
🎯 Key Insight: A sorted rotated array has at most one position where the order breaks in circular fashion
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code