|
| 1 | +--- |
| 2 | +created: 2026-01-25 |
| 3 | +modified: |
| 4 | +completed: false |
| 5 | +leetcode-index: 1752 |
| 6 | +link: https://leetcode.com/problems/check-if-array-is-sorted-and-rotated |
| 7 | +difficulty: Easy |
| 8 | +tags: |
| 9 | + - cp/array |
| 10 | + - leetcode |
| 11 | +--- |
| 12 | +# Check if Array Is Sorted and Rotated |
| 13 | + |
| 14 | +## Question |
| 15 | +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`. |
| 16 | + |
| 17 | +There may be duplicates in the original array. |
| 18 | + |
| 19 | +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`. |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +>[!Example]+ Example 1 |
| 24 | +>**Input**: `nums = [3,4,5,1,2]` |
| 25 | +>**Output**: `true` |
| 26 | +>**Explanation**: |
| 27 | +>[1,2,3,4,5] is the original sorted array. You can rotate the array by x = 2 positions to begin on the element of value 3: [3,4,5,1,2]. |
| 28 | +
|
| 29 | +>[!Example]+ Example 2 |
| 30 | +>**Input**: `nums = [2,1,3,4]` |
| 31 | +>**Output**: `false` |
| 32 | +>**Explanation**: |
| 33 | +>There is no sorted array once rotated that can make nums. |
| 34 | +
|
| 35 | +>[!Example]+ Example 3 |
| 36 | +>**Input**: `nums = [1,2,3]` |
| 37 | +>**Output**: `true` |
| 38 | +>**Explanation**: |
| 39 | +>[1,2,3] is the original sorted array. You can rotate the array by x = 0 positions (i.e. no rotation) to make nums. |
| 40 | +
|
| 41 | +>[!warning]+ Constraints |
| 42 | +>- `1 <= nums.length <= 100` |
| 43 | +> |
| 44 | +>- `1 <= nums[i] <= 100` |
| 45 | +### Hints |
| 46 | +>[!Hint]- Hint 1 |
| 47 | +>Brute force and check if it is possible for a sorted array to start from each position. |
| 48 | +
|
| 49 | +--- |
| 50 | +## Solution |
| 51 | + |
| 52 | +### Intuition |
| 53 | + |
| 54 | +A sorted array has **no places where the order decreases**. |
| 55 | +If such an array is rotated, there will be **exactly one place** where the order breaks. |
| 56 | + |
| 57 | +So instead of simulating rotations, I can simply count how many times: |
| 58 | + |
| 59 | +nums[i] > nums[i + 1] |
| 60 | + |
| 61 | +occurs while traversing the array. |
| 62 | + |
| 63 | +- `0` breaks → already sorted |
| 64 | +- `1` break → sorted and rotated |
| 65 | +- `>1` breaks → not possible by rotation |
| 66 | + |
| 67 | + |
| 68 | +### Approach |
| 69 | + |
| 70 | +1. Traverse the array once. |
| 71 | +2. Count the number of indices `i` such that |
| 72 | + `nums[i] > nums[(i + 1) % n]`. |
| 73 | +3. If this count exceeds `1`, return `false`. |
| 74 | +4. Otherwise, return `true`. |
| 75 | + |
| 76 | +The modulo allows comparison of the **last element with the first**, which is required because rotation is circular. |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +### Complexity |
| 81 | + |
| 82 | +#### Time complexity |
| 83 | +- **O(n)** — a single pass through the array. |
| 84 | + |
| 85 | +#### Space complexity |
| 86 | +- **O(1)** — no extra space used. |
| 87 | + |
| 88 | +### Code |
| 89 | +--- |
| 90 | +```cpp |
| 91 | +// Conceptual brute force idea: |
| 92 | +// Sort the array, then try all rotations and compare. |
| 93 | +// This is not implemented here because it is unnecessary and inefficient. |
| 94 | +``` |
| 95 | +--- |
| 96 | +### Optimal Code |
| 97 | +--- |
| 98 | +```cpp |
| 99 | +class Solution { |
| 100 | +public: |
| 101 | + bool check(vector<int>& nums) { |
| 102 | + int n = nums.size(); |
| 103 | + int drops = 0; |
| 104 | + |
| 105 | + for (int i = 0; i < n; i++) { |
| 106 | + if (nums[i] > nums[(i + 1) % n]) { |
| 107 | + drops++; |
| 108 | + if (drops > 1) |
| 109 | + return false; |
| 110 | + } |
| 111 | + } |
| 112 | + return true; |
| 113 | + } |
| 114 | +}; |
| 115 | +``` |
| 116 | + |
| 117 | +## Key Takeaway |
| 118 | + |
| 119 | +> A sorted and rotated array can have **at most one inversion** when treated as circular. |
0 commit comments