Full detailed explanation without sorting and without any extra space.
Intuition\nWhen array is sorted then it\'s previous element should be smaller than the next one i.e. nums[i-1]numsi. If we find more than one such i\'s then str
# Intuition\nWhen array is sorted then it\'s previous element should be smaller than the next one i.e. nums[i-1]<nums[i].\n\n# Approach\nIf we carefully observe then we will get to know if the array is not sorted and it can be made sorted by right shifts then there should be single i only for which nums[i-1]>nums[i]( i.e. violatiing the condition for sorted array). If we find more than one such i\'s then straight away return -1. One more thing that when i==0 then we can\'t simply do i-1 we need to compare it with last element to get that we do (i-1+n)%n. Also there might be the case that array is already sorted so we will get the answer as length of array cuz our ans will always be ans=n-i so in order to avoid that we return ans%n\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\npublic:\n    int minimumRightShifts(vector<int>& nums) {\n        int ans=INT_MAX;\n        int n=nums.size();\n        for(int i=0;i<n;i++){\n            int prev=(i-1+n)%n;\n            if(nums[prev]>nums[i]){\n                if(ans==INT_MAX)\n                    ans=n-i;\n                else\n                    return -1;\n            }\n           \n        }\n        return ans%n;\n    }\n};\n```
['Array', 'C++']