Easy 3 lines 🔥| Detailed explanation ☑️☑️ | Reverse approach🚀 ( Striver's approach :) )
Approach\n1. First, we calculate the effective rotation steps k by taking the modulus of k with the size of the array n. This ensures that k is within the range
# Approach\n1. First, we calculate the effective rotation steps k by taking the modulus of k with the size of the array n. This ensures that k is within the range [0, n).\n2. We then reverse the entire array. This effectively moves the last k elements to the front.\n3. After reversing, the first k elements in the array are the elements that should be at the end of the rotated array. So, we reverse only the first k elements to bring them to their correct positions.\n4. Finally, we reverse the rest of the elements in the array after the first k elements. This brings the remaining elements back to their correct positions after rotation.\n<!-- Describe your approach to solving the problem. -->\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```\n\nclass Solution {\npublic:\n    void rotate(vector<int>& nums, int k) {\n        int n = nums.size();\n        k = k % n; // Ensure k is within the range [0, n)\n\n        // Reverse the entire array\n        reverse(nums.begin(), nums.end());\n        \n        // Reverse the first k elements\n        reverse(nums.begin(), nums.begin() + k);\n        \n        // Reverse the rest of the elements after k\n        reverse(nums.begin() + k, nums.end());\n    }\n};\n\n```\n![upvote.jpg](https://assets.leetcode.com/users/images/1d6c3ba1-09fd-4d32-aea7-6fab48eed29e_1709351782.2976372.jpeg)\n
['Array', 'C++']