O(n) Solution✅Easy To Understand💯🚀
Nearly identical problem :1752. Check if Array Is Sorted and RotatedComplexity :
Time complexity: O(n)

Space complexity: O(1)
Code :
![image.png](https://assets.leetcode.com/users/images/b256cddb-cb2e-488e-a77a-8fa3d1ce3e97_1738495653.6012697.png)


----
## Nearly identical problem :

# [1752. Check if Array Is Sorted and Rotated](https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/description/?envType=daily-question&envId=2025-02-02)

---

# Complexity :
- Time complexity: $O(n)$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $O(1)$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
----
# Code :
```cpp []
class Solution {
public:
    int minimumRightShifts(vector<int>& nums) {
        int n = nums.size();
        int i = 1;
        while(i < n && nums[i] >= nums[i - 1]) {
            ++i;
        }
        if(i == n)
            return 0;
        
        int ans = i;
        ++i;
        while(i < n && nums[i] >= nums[i - 1]) {
            ++i;
        }
        if(i != n)
            return -1;
        if(nums.back() <= nums[0])
            return n - ans;
        return -1;
     }
};
```
['Array', 'Greedy', 'C++']