[Java/C++/Python] A very-very well detailed explanation
\nWell, to be honest with you all. This is really not a hard problem! It\'s an easy one\n\n\nAlright, what the question is saying that we have given an array & 
```\nWell, to be honest with you all. This is really not a hard problem! It\'s an easy one\n```\n\nAlright, what the question is saying that we have **given an array & we have to rotate the array to the right by k steps, where k is non-negative.**\n```\nOkay so, we have one thing that k will always be > 0.\nBut, I will teach you, one bonus point as well what if k < 0 i.e. k is -ve then how can you rotate the array.\n```\nLet\'s undertsand this problem using an example,\n**Input:** nums = [1,2,3,4,5,6,7], k = 3\n**Output:** [5,6,7,1,2,3,4]\n\n```\n"K all possible rotation"\n\n[7,1,2,3,4,5,6], k = 1\n[6,7,1,2,3,4,5], k = 2\n[5,6,7,1,2,3,4], k = 3\n[4,5,6,7,1,2,3], k = 4\n[3,4,5,6,7,1,2], k = 5\n[2,3,4,5,6,7,1], k = 6\n[1,2,3,4,5,6,7], k = 7\n```\n\nWe have **k is 3**, so it means we have to take **3 values from the back** and **put in the front** of the array values.\n\nSo, for that what we will do is, we will break Array into 2 parts. **Part1[P1] & Part2[P2]**\n* `[P1] is defined as` the array part just before the last 3 values. What I mean is something like [1,2,3,4]\n* `[P2] is defined as` the array part just after remaining values which we have to rotate [5,6,7]\n\n![image](https://assets.leetcode.com/users/images/bafbf0fc-198b-4fe2-b4e9-974330840daf_1643513368.0827003.png)\n\nSo, in order to rotate this Array **k times** what we will do is, we will reverse the **P1 first which become [4,3,2,1]** & then we **reverse P2 which becomes [7,6,5]**\nNow finally what we have to do is we gonna **reverse the complete array** by doing that what will happen is our array become **[5,6,7,1,2,3,4]** and that\'s what we want in our **Output**\n\n![image](https://assets.leetcode.com/users/images/b303db4f-a485-41ef-9b0a-1e5141c634d3_1643514374.4636352.png)\n\nBut, what if we have **k = 101**, then we will **not rotate it** 101 times. It simply means **till 100 times it will be [1,2,3,4,5,6,7]** & we have to **rotate only 1 time i.e. [7,1,2,3,4,5,6]**. So, now your question is how can we handle this, we simply do the **modulo of "k"** with length of array\n```\nOkay Bonus point what if we have k = -1, then how can we rotate the array. If k is -1 then we have to rotate the value backward not in the front.\nEg - \nInput : [1,2,3,4,5,6,7], k = -1\nOutput : [2,3,4,5,6,7,1]\n\nNow how did we figure out this, if you carefully look that k = -1 is equals to k = 6.\nJust look at the table which I have made for every possible k values\n\nSo, what It represent is that add the -ve value to the length of array. And you will get your answer!\n```\n\n*I hope Approach is crystal clear now!* **Let\'s do some dirty work**, `"code it up"`\n\n*code each line explained :* `Similar for C++, Java, Python` **{Only Syntax Difference}** approach same\n* Step - 1\n```\n// reversing the array values\n    public static void reverse(int nums[], int i, int j){\n        int li = i; // left index;\n        int ri = j; // right index\n        \n        while(li < ri){\n            int temp = nums[li];\n            nums[li] = nums[ri];\n            nums[ri] = temp;\n            \n            li++;\n            ri--;\n        }\n    }\n```\n* Step - 2\n```\npublic void rotate(int[] nums, int k) {\n        k = k % nums.length; // if we have let\'s say 101 to rotate, then we only rotate it 1 time not 101 times\n        if(k < 0){ // if we get -ve value, then -ve is just equals to it\'s -ve + array.length\n            k += nums.length;\n        }\n        // part 1 reverse\n        reverse(nums, 0, nums.length - k - 1);\n        // part 2 reverse\n        reverse(nums, nums.length - k, nums.length - 1);\n        // complete reverse\n        reverse(nums, 0, nums.length - 1);\n    }\n```\n\n**Java**\n```\nclass Solution {\n\t public static void reverse(int nums[], int i, int j){\n        int li = i;\n        int ri = j;\n        \n        while(li < ri){\n            int temp = nums[li];\n            nums[li] = nums[ri];\n            nums[ri] = temp;\n            \n            li++;\n            ri--;\n        }\n    }\n    public void rotate(int[] nums, int k) {\n        k = k % nums.length; \n        if(k < 0){ \n            k += nums.length;\n        }\n        reverse(nums, 0, nums.length - k - 1);\n        reverse(nums, nums.length - k, nums.length - 1);\n        reverse(nums, 0, nums.length - 1);\n    }\n}\n```\n**C++**\n```\nclass Solution {\n    public :\n    void reverse(vector<int>& nums, int i, int j){\n        int li = i; // left;\n        int ri = j;\n        \n        while(li < ri){\n            int temp = nums[li];\n            nums[li] = nums[ri];\n            nums[ri] = temp;\n            \n            li++;\n            ri--;\n        }\n    }\n    void rotate(vector<int>& nums, int k) {\n        k = k % nums.size();\n        if(k < 0){ \n            k += nums.size();\n        }\n        reverse(nums, 0, nums.size() - k - 1);\n        reverse(nums, nums.size() - k, nums.size() - 1);\n        reverse(nums, 0, nums.size() - 1);\n    }\n};\n```\n**Python**\n```\nclass Solution:\n    def reverse (self, nums, i, j) : \n        li = i\n        ri = j\n        \n        while li < ri:\n            temp = nums[li]\n            nums[li] = nums[ri]\n            nums[ri] = temp\n            \n            li += 1\n            ri -= 1\n    def rotate(self, nums: List[int], k: int) -> None:\n        """\n        Do not return anything, modify nums in-place instead.\n        """\n        k = k % len(nums)\n        if k < 0 : \n            k += len(nums)\n        \n        self.reverse(nums, 0, len(nums) - k - 1);\n        self.reverse(nums, len(nums) - k, len(nums) - 1);\n        self.reverse(nums, 0, len(nums) - 1);\n```\nANALYSIS :-\n* **Time Complexity :-** BigO(N)\n\n* **Space Complexity :-** BigO(1)
['C', 'Python', 'Java']