✅☑[C++] ||  2 Approaches  || Explained🔥
PLEASE UPVOTE GUYS\uD83E\uDD79\n# Approach\nExplained in the code\n\n\n1. Input Check:\nThe code takes an input vector called nums.\n\n1. Outer Loop:\nIt starts
# PLEASE UPVOTE GUYS\uD83E\uDD79\n# Approach\n***Explained in the code***\n\n\n1. **Input Check:**\nThe code takes an input vector called nums.\n\n1. **Outer Loop:**\nIt starts an outer loop that will iterate through each possible number of right shifts (0 to nums.size() - 1).\n\n1. **Check if Sorted:**\nIn each iteration of the outer loop, it checks if the current state of the vector is already sorted in non-decreasing order.\n\n1. **Inner Loop - Right Shift:**\nIf the vector is not sorted, it enters an inner loop that performs a right shift.\nThe inner loop swaps adjacent elements from the end of the vector to the beginning, effectively moving the last element to the first position.\n\n1. **Continue Shifting:**\nAfter each right shift, it checks again if the vector is sorted.\nIf it\'s not sorted, it continues the inner loop, performing another right shift.\n\n1. **Exit on Sorted Vector:**\nWhen the inner loop completes, the vector is checked again for sorting.\nIf it\'s now sorted, the code returns the number of shifts performed (the current value of the outer loop index).\n\n1. **Exit with -1 if Not Sorted:**\nIf all possible right shifts are performed, and the vector is still not sorted, it means it\'s not possible to sort it using right shifts, and the code returns -1.\n\n1. **Result:**\nThe result is the minimum number of right shifts required to sort the vector, or -1 if it\'s not possible to sort it this way.\n\n# Complexity\n- Time complexity:\n**O(n^2)**\n\n- Space complexity:\n**O(1)**\n\n# Code\n```\n                ---------------Approach 1---------------\nclass Solution {\npublic:\n    // This function checks if the given vector is sorted in non-decreasing order\n    bool check(vector<int> nums){\n        for(int i = 0; i + 1 < nums.size(); i++){\n            if(nums[i] > nums[i+1])\n                return false; // If any element is greater than its next element, return false\n        }\n        return true; // If the loop completes without finding an out-of-order element, return true\n    }\n\n    // This function calculates the minimum right shifts required to sort the vector\n    int minimumRightShifts(vector<int>& nums) {\n        for(int i = 0; i < nums.size(); i++){\n            if(check(nums))\n                return i; // If the vector is already sorted, return the number of shifts performed\n            for(int j = nums.size() - 1; j > 0; j--){\n                swap(nums[j], nums[j-1]); // Right shift the elements by swapping adjacent elements\n            }\n        }\n        return -1; // If it\'s not possible to sort the vector with shifts, return -1\n    }\n};\n                ---------------Approach 2---------------\n\nclass Solution {\npublic:\n    // Helper function to reverse a portion of the vector \'temp\'\n    void reverse(vector<int> &temp, int l, int h) {\n        while (l < h) {\n            swap(temp[l], temp[h]);\n            l++;\n            h--;\n        }\n    }\n\n    // Function to find the minimum number of right shifts to sort the vector \'nums\'\n    int minimumRightShifts(vector<int>& nums) {\n        vector<int> temp = nums; // Create a copy of the input vector \'nums\'\n        int index = -1; // Initialize the \'index\' variable to -1\n        sort(temp.begin(), temp.end()); // Sort the \'temp\' vector in ascending order\n        reverse(nums, 0, nums.size() - 1); // Reverse the \'nums\' vector\n\n        // Find the index where the original \'nums\' vector starts becoming unsorted\n        for (int i = 1; i < nums.size(); i++) {\n            if (nums[i] > nums[i - 1]) {\n                index = i - 1;\n                break;\n            }\n        }\n\n        // Reverse the first portion up to \'index\' and the second portion after \'index\'\n        reverse(nums, 0, index);\n        reverse(nums, index + 1, nums.size() - 1);\n\n        // Check if \'temp\' (sorted \'nums\') is equal to the modified \'nums\' vector\n        // If they are equal, return \'index + 1\' (number of right shifts needed), otherwise return -1\n        return (temp == nums) ? index + 1 : -1;\n    }\n};\n\n\n```
['C++']