C++/Java/Python/JavaScript|| ✅🚀2 Approaches ( Brute and  ✔️🔥Optimized Space complexity: O(1))
1. Brute Force: Time complexity: O(n), Space complexity: O(k)\n### Intuition\nThe intuition behind the approach is to divide the rotation process into two steps
# 1. Brute Force: Time complexity: O(n), Space complexity: O(k)\n### Intuition\nThe intuition behind the approach is to divide the rotation process into two steps: first, storing the last k elements in a temporary array, and then rearranging the remaining elements in nums to their new positions. By using this two-step process, the function achieves the desired rotation effect.\n\n### Approach\n1. Initialize the variable `n` with the size of the `nums` vector.\n\n2. The value of `k` is updated to be the remainder when divided by `n`. This step ensures that `k` is within the valid range of indices.\n\n3. If `k` is greater than `n`, the function returns early without performing any rotations. This is because rotating by `k` positions would result in the same configuration as rotating by `k % n` positions.\n\n4. Next, the function checks for some special cases where rotation is not necessary: when `n` or `k` is 0, or when `n` is equal to `k`. In such cases, the function returns early without any further computations.\n\n5. If none of the early return conditions are met, an array `temp` of size `k` is created to store the last `k` elements of `nums` temporarily.\n\n6. A loop runs `k` times, copying the last `k` elements of `nums` to the `temp` array in reverse order.\n\n7. Another loop runs from `n-1` to 0. For each iteration, if the index `i` is less than `k`, the corresponding element in `nums` is updated with the element from `temp` at the reversed index `k-i-1`. This step places the rotated elements at the beginning of `nums`.\n\n8. If the index `i` is greater than or equal to `k`, the corresponding element in `nums` is updated with the element at index `i-k`, effectively shifting the remaining elements to their new positions.\n\n9. After the loops complete, the function has successfully rotated the elements in `nums` by `k` positions.\n\n### Complexity\n- Time complexity: O(n), where n is the size of the input vector `nums`. This is because the code performs two loops, one with `k` iterations and the other with `n` iterations, where `k` and `n` are at most equal to the size of `nums`. All other operations are constant time.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(k), as it uses an auxiliary array `temp` of size `k` to temporarily store the elements to be rotated.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    void rotate(vector<int>& nums, int k) {\n        int n = nums.size();\n\t   k = k % n;\n\t   cout<<k<<endl;\n\t   if (k > n){\n        return;\n\t   }\n        if(n==0 || k==0 || n==k){\n\t       return;\n\t   }\n\t   int temp[k];\n\t   for(int i=0;i<k;i++){\n\t       temp[i]=nums[n-i-1];\n\t   }\n\t   for(int i=n-1;i>=0;i--){\n\t\t\t if(i<k){\n\t\t\t\t nums[i]=temp[k-i-1];\n\t\t\t }\n\t\t\t else{\n\t\t\t\t nums[i]=nums[i-k];\n\t\t\t }\n\t   }\n    }\n};\n```\n\n---\n\n\n# 2. Optimized Approach : Time complexity: O(n), Space complexity: O(1)\n\n# Intuition:\nThe problem requires rotating the elements of a vector to the right by a given number of positions. To optimize the code to achieve O(1) space complexity, we can perform the rotation in-place without using any additional arrays.\n\n# Approach:\n1. First, we initialize the variables `n` and `k`, where `n` is the size of the input vector `nums`, and `k` is the number of positions to rotate.\n2. We update `k` to be the remainder when divided by `n` to ensure it is within the valid range of indices.\n3. We handle some special cases: if `k` is 0, `n` is 0, or `n` is equal to `k`, we return early as no rotation is needed.\n4. We initialize the variables `count`, `start`, `current`, and `prev`. The `count` keeps track of the number of elements moved, `start` represents the starting index for each rotation cycle, `current` is the current index being processed, and `prev` stores the value that will be moved to the next position.\n5. We enter a loop that continues until all elements have been moved to their correct positions. Inside the loop:\n   - We calculate the new index `current` by adding `k` to the current index and taking the modulus of `n` to handle wraparound.\n   - We swap the value at the current index `current` with the value in `prev`.\n   - We update `prev` to the value that was previously at the current index.\n   - We increment the `count`.\n   - If the `current` index becomes equal to the `start` index, it means that we have completed a rotation cycle. We increment `start`, update `current` and `prev` to the new values, and continue the rotation process until all elements have been moved.\n6. Once the loop completes, the vector `nums` will contain the rotated elements.\n\n# Complexity:\n- The time complexity of the optimized code is O(n), where n is the size of the input vector `nums`. This is because the code needs to visit each element in the vector to perform the rotations.\n\n- The space complexity of the optimized code is O(1) since it does not require any additional space proportional to the input size. It only uses a few extra variables to keep track of indices and temporarily store values during swapping.\n# C++\n```cpp\nclass Solution {\npublic:\n    void rotate(vector<int>& nums, int k) {\n        int n = nums.size();\n        k = k % n;\n        if (k == 0 || n == 0 || n == k) {\n            return;\n        }\n        \n        int count = 0;\n        int start = 0;\n        int current = start;\n        int prev = nums[start];\n        \n        while (count < n) {\n            current = (current + k) % n;\n            int temp = nums[current];\n            nums[current] = prev;\n            prev = temp;\n            count++;\n            \n            if (current == start) {\n                start++;\n                current = start;\n                prev = nums[start];\n            }\n        }\n    }\n};\n```\n# Java\n```\nclass Solution {\n    public void rotate(int[] nums, int k) {\n        int n = nums.length;\n        k = k % n;\n        if (k == 0 || n == 0 || n == k) {\n            return;\n        }\n        \n        int count = 0;\n        int start = 0;\n        int current = start;\n        int prev = nums[start];\n        \n        while (count < n) {\n            current = (current + k) % n;\n            int temp = nums[current];\n            nums[current] = prev;\n            prev = temp;\n            count++;\n            \n            if (current == start) {\n                start++;\n                current = start;\n                prev = nums[start];\n            }\n        }\n    }\n}\n\n```\n# Python\n```\nclass Solution(object):\n    def rotate(self, nums, k):\n        n = len(nums)\n        k = k % n\n        if k == 0 or n == 0 or n == k:\n            return\n        \n        count = 0\n        start = 0\n        current = start\n        prev = nums[start]\n        \n        while count < n:\n            current = (current + k) % n\n            nums[current], prev = prev, nums[current]\n            count += 1\n            \n            if current == start:\n                start += 1\n                current = start\n                prev = nums[start]\n\n```\n# JavaScript\n```\nvar rotate = function(nums, k) {\n    var n = nums.length;\n    k = k % n;\n    if (k === 0 || n === 0 || n === k) {\n        return;\n    }\n    \n    var count = 0;\n    var start = 0;\n    var current = start;\n    var prev = nums[start];\n    \n    while (count < n) {\n        current = (current + k) % n;\n        var temp = nums[current];\n        nums[current] = prev;\n        prev = temp;\n        count++;\n        \n        if (current === start) {\n            start++;\n            current = start;\n            prev = nums[start];\n        }\n    }\n};\n\n```
['Python', 'C++', 'Java', 'JavaScript']