👏Beats 100.00% of users with Java || Simple & Easy Array, Swap & Two Pointer Approach 🔥
Intuition\nReverse the entire array, after that reverse first k elements and reverse k+1 to nums.length-1 elements.\n\n# I Think This Can Help You\n\n\n\n# Appr
# Intuition\nReverse the entire array, after that reverse first k elements and reverse k+1 to nums.length-1 elements.\n\n# I Think This Can Help You\n![Screenshot 2024-02-24 232407.png](https://assets.leetcode.com/users/images/45323e76-a3a5-47b9-8928-4b90fffa377d_1710828534.3350306.png)\n\n\n# Approach\n1. Create a swap function to swap to array elements.\n2. Create a reverse function to reverse array from ith position to jth position.\n3. find reminder of k divided by nums.length because k is more than nums.length we don\'t need to iterate huge iterations.\n3. first of all reverse entire the array.\n4. reverse the array from 0th index to k-1 index.\n5. reverse the array from kth index to araay.length-1 index.\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\n    public void rotate(int[] nums, int k) {\n        k = k % nums.length;   // find reminder of k divided by nums.length because k is more than nums.length we don\'t need to iterate huge iterations.\n        reverse(nums,0,nums.length-1);  // first of all reverse entire the array.\n        reverse(nums,0,k-1);   // reverse the array from 0th index to k-1 index.\n        reverse(nums,k,nums.length-1);   // reverse the array from kth index to araay.length-1 index.\n    }\n    void reverse(int[] nums,int i,int j){   // Create a reverse function to reverse array from ith position to jth position.\n        while(i<j){\n            swap(nums,i,j);\n            i++;\n            j--;\n        }\n    }\n    void swap(int[] nums,int i,int j){   // Create a swap function to swap to array elements.\n        int temp = nums[i];\n        nums[i] = nums[j];\n        nums[j] = temp;\n    }\n}\n```\n\n\n![_4613fad8-dfaf-49b7-a06e-3a0dd6756db2.jpeg](https://assets.leetcode.com/users/images/ca7cac5f-5756-49b9-b173-66012716747c_1709272428.4311576.jpeg)\n
['Array', 'Math', 'Two Pointers', 'Java']