✅Beats 100%✅|| C++ || Single pass (8 lines) || Detailed Explaination || Time: O(n) || Space: O(1)
Intuition\n Describe your first thoughts on how to solve this problem. \nWe scan the array looking for an inflection point (nums[i]>nums[i+1]) in the array.\n\n
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe scan the array looking for an inflection point <em><strong>(nums[i]>nums[i+1])</em></strong> in the array.\n\nWe do this because with the ability of right shifting elements of the array, <em><strong>we can only deal with ATMOST 1 INFLECTION POINT.</em></strong>\n\nThis is beacause if we have a point in the array, where (nums[i]>nums[i+1]), we can resolve this by shifting nums[i] to the very end, and have nums[i+1] bumped to the start of the array (right-shifted), thus removing the comparision between them altogether. If we get more than 1 such point, it will be impossible to sort the array.\n\n(You can imagine a strictly increasing array as a circular loop having exactly 1 inflection point between the starting element and last element of the array)\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe loop through the array, counting up the inflection points, and the positions. Since after shifting, nums[n-1] and nums[0] will be involved in comparison, they need to be checked as well.\n\nThe 3 cases generated by the number of inflection points:\n- If more than 1, return -1 (not possible to sort)\n- If equal to 1, we can shift the array by (n-i-1)\n- If equal to 0, no need to shift, return 0\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int minimumRightShifts(vector<int>& nums) {\n        int n=nums.size(), count=0, shifts=0;\n        \n        for (int i=0; i<n; i++)\n            if (nums[i]>nums[(i+1)%n]){\n                count++; \n                shifts=n-i-1;\n            }\n\n        if (count<2) \n            return shifts;\n        return -1;\n    }\n};\n```
['Counting', 'C++']