The problem is to determine whether the given array could be a non-decreasing, sorted array after at most one rotation. A rotated sorted array can have only one point where the order "breaks" or decreases. This means there can be at most one instance where a larger element is followed by a smaller one.
1, the array cannot be a valid rotated sorted array.1 or less, the array can be considered sorted with at most one rotation. Otherwise, it’s not possible.Here’s the code:
class Solution {
public:
bool check(vector<int>& nums) {
int n=nums.size();
int cnt=0;
for(int i=1;i<n;i++){
if(nums[i-1]>nums[i]){
cnt++;
}
}
if(nums[n-1]>nums[0])cnt++;
if(cnt>1) return false;
return true;
}
};
Feel free to share your approach and feedback in comment box.