C++ (Fully Explained)  ||  Time: O(n) ||  Space: O(1)
\n\n# Code\n\nclass Solution {\nprivate:\n    bool isRotatedSorted(vector<int> &nums) {\n        int count=0;\n        // The above initiated variable \'count\'
\n\n# Code\n```\nclass Solution {\nprivate:\n    bool isRotatedSorted(vector<int> &nums) {\n        int count=0;\n        // The above initiated variable \'count\' will store the number of times the next element of nums becomes less than current element. ie,\n        // For example, if nums is: 4 2 3 0 => count=2; as there is a decrease from 4 to 2 and 3 to 0.\n        // if nums is: 1 2 3 4 => count will be 0 as there is no decrease.\n        // The following for loop calculates the value of count.\n        for(int i=0; i<nums.size()-1; i++) {\n            if(nums[i]>nums[i+1]) count++;\n        }\n        // (count==0) This is the Condition for already sorted array.\n        // And, (count==1 && nums[0]>nums[nums.size()-1])) This condition is for rotated sorted array.\n        if(count==0||(count==1 && nums[0]>nums[nums.size()-1])) return true;\n        return false;\n    }\npublic:\n    int minimumRightShifts(vector<int>& nums) {\n        bool check=isRotatedSorted(nums);\n        // The Above called funtion checks if nums is already a sorted array or it is a rotated sorted array,in both two cases it returns true, otherwise it returns false.\n        if(check==false) return -1;\n        // If check is false, so no need to move forward because if it is niether sorted and nor rotated sorted it is not possible to sort it by the given rule.So, just return -1.\n        int n=nums.size(),ans=0;\n        // If nums is already sorted then, ans will be 0 or if it is a rotated sorted array, in such case ans is calculated by the following code:\n        // Let\'s take a rotated sorted nums for example:\n        // nums: 4 5 1 2 3.\n        // Initialy we will point on 3(the last element). since, 3 < 4(the first element), we will increment the \'ans\' and we will go back.\n        // now, we will point on 2(second last element), still 2 < 4, we will increment the \'ans\' and we will move back again.\n        // same for 1, 1 < 4, we will increment the \'ans\' and we will go back again.\n        // now, we will point on 5, since 5 > 4 we will stop \n        // The following for loop works on the same mechanism.\n        for(int i=n-1; i>0; i--) {\n            if(nums[i]<nums[0]) ans++;\n            else break;\n        }\n        // Finally, ans is returned.\n        return ans;\n    }\n};\n// upvote if it helps.\n```
['Array', 'Sorting', 'Counting', 'C++']