Recognize a Rotation of Nondecreasing Values. Return whether nums can be obtained by rotating a nondecreasing array by some number of positions, including zero. Duplicate values are allowed.
### Problem
Return whether `nums` can be obtained by rotating a nondecreasing array by some number of positions, including zero. Duplicate values are allowed. A rotation of A has entries `B[i] = A[(i+x) % A.length]` for one common shift x.
```cpp
class Solution {
public:
bool check(vector<int>& nums);
};
```
### Examples
**Example 1:**
```text
nums = [4,4,1,2,4]
Output: true
Explanation: It is a rotation of [1,2,4,4,4].
```
**Example 2:**
```text
nums = [3,1,4,2]
Output: false
Explanation: Circular traversal contains two descending transitions: 3 to 1 and 4 to 2.
```
### Constraints
- `1 <= nums.length <= 100`.
- `1 <= nums[i] <= 100`.
### Clarifying Questions
Nondecreasing allows equality. Include the last-to-first edge when analyzing the array circularly.
```hint Inspect the structure
What does the supplied rotation definition permit when values are equal or the shift is zero?
```
### Reasoning checkpoints
- Establish both directions of the claimed rotation test.
- Handle duplicate values and the circular boundary without changing the nondecreasing requirement.
### Optional extensions
How would you return a valid rotation shift rather than just a Boolean?
Overview: Recognize a Rotation of Nondecreasing Values. Return whether nums can be obtained by rotating a nondecreasing array by some number of positions, including zero. Duplicate values are allowed.
Return whether nums can be obtained by rotating a nondecreasing array by some number of positions, including zero. Duplicate values are allowed. A rotation of A has entries B[i] = A[(i+x) % A.length] for one common shift x.
class Solution {
public:
bool check(vector<int>& nums);
};
Examples
Example 1:
nums = [4,4,1,2,4]
Output: true
Explanation: It is a rotation of [1,2,4,4,4].
Example 2:
nums = [3,1,4,2]
Output: false
Explanation: Circular traversal contains two descending transitions: 3 to 1 and 4 to 2.
Constraints
1 <= nums.length <= 100
.
1 <= nums[i] <= 100
.
Clarifying Questions Guidance
Nondecreasing allows equality. Include the last-to-first edge when analyzing the array circularly.
Reasoning checkpoints
Establish both directions of the claimed rotation test.
Handle duplicate values and the circular boundary without changing the nondecreasing requirement.
Optional extensions
How would you return a valid rotation shift rather than just a Boolean?