Java - O(N) Time & O(1) Space with examples
Example:\n\n\ninput = {1, 2, 3, 4, 5, 6, 7}\nk = 3\n\n// Expected output\noutput = {5, 6, 7, 1, 2, 3, 4}\n\n// Step 1: Reverse the entire array\n{7, 6, 5, 4, 3,
**Example:**\n\n```\ninput = {1, 2, 3, 4, 5, 6, 7}\nk = 3\n\n// Expected output\noutput = {5, 6, 7, 1, 2, 3, 4}\n\n// Step 1: Reverse the entire array\n{7, 6, 5, 4, 3, 2, 1}\n// Step 2: Reverse 0 to k-1 elements\n{5, 6, 7, 4, 3, 2, 1}\n \\____/\n// Step 3: Reverse from k to n-1 elements\n{5, 6, 7, 1, 2, 3, 4}\n           \\______/\n```\n\n**Code:**\n```java\nclass Solution {\n    public void rotate(int[] nums, int k) {\n        k %= nums.length;\n        if (k == 0) return; // Rotated array will be same as original array\n        reverse(0, nums.length-1, nums);\n        reverse(0, k-1, nums);\n        reverse(k, nums.length-1, nums);\n    }\n    \n    private void reverse(int start, int end, int[] nums){        \n        while(start < end){\n            int temp = nums[start];\n            nums[start++] = nums[end];\n            nums[end--] = temp;\n        }\n    }\n}\n```
['Java']