⚡2 CODE SOLUTIONS⚡ (with & without Reversal)📗| BEATS 100%✅ [C++/Java/Python/Javascript]🪟
SOLUTION 1 :IntuitionThe goal is to rotate an array nums to the right by k positions. We split the array into two parts:
The last k elements.
The remaining n-k
# SOLUTION 1 :
# Intuition

The goal is to rotate an array `nums` to the right by `k` positions. We split the array into two parts:
1. The last `k` elements.
2. The remaining `n-k` elements.

The last `k` elements are moved to the front, and the other elements are shifted `k` positions to the right.

# Approach

1. **Modulo Operation**: First, adjust `k` to ensure it’s within bounds, i.e., `k = k % n`.
2. **Store the last `k` elements**: Save the last `k` elements in a temporary array.
3. **Shift the first `n-k` elements**: Move the elements before the last `k` to the right by `k` positions.
4. **Place the last `k` elements**: Copy the elements stored in the temporary array to the front of the array.

# Complexity

- **Time Complexity**: \(O(n)\), because we traverse the array twice.
- **Space Complexity**: \(O(k)\), due to the temporary array that holds the last `k` elements.

# Code

```cpp []
class Solution {
public:
    void rotate(std::vector<int>& nums, int k) {
        int n = nums.size();
        k = k % n;
        if (k == 0) return;

        vector<int> temp(nums.end() - k, nums.end());
        for (int i = n - k - 1; i >= 0; i--) {
            nums[i + k] = nums[i];
        }
        for (int i = 0; i < k; i++) {
            nums[i] = temp[i];
        }
    }
};
```
```python []
class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        n = len(nums)
        k = k % n
        if k == 0: return
        
        temp = nums[-k:]
        nums[k:] = nums[:-k]
        nums[:k] = temp
```

```java []
class Solution {
    public void rotate(int[] nums, int k) {
        int n = nums.length;
        k = k % n;
        if (k == 0) return;
        
        int[] temp = new int[k];
        System.arraycopy(nums, n - k, temp, 0, k);
        
        for (int i = n - k - 1; i >= 0; i--) {
            nums[i + k] = nums[i];
        }
        
        System.arraycopy(temp, 0, nums, 0, k);
    }
}
```
```javascript []
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {void} Do not return anything, modify nums in-place instead.
 */
var rotate = function(nums, k) {
    let n = nums.length;
    k = k % n;
    if (k === 0) return;
    
    let temp = nums.slice(-k);
    for (let i = n - k - 1; i >= 0; i--) {
        nums[i + k] = nums[i];
    }
    for (let i = 0; i < k; i++) {
        nums[i] = temp[i];
    }
};
```

# SOLUTION 2 :
# Intuition

The goal is to rotate an array `nums` to the right by `k` positions. A common approach is to reverse parts of the array to achieve this efficiently. By reversing the entire array, then reversing the first `k` elements and the remaining part of the array, we can get the rotated array.

# Approach

1. **Modulo Operation**: First, calculate `k % n` to handle cases where `k` is larger than the length of the array. This ensures the rotation is within bounds.
2. **Reverse the Entire Array**: Reverse the entire array to prepare for rotating.
3. **Reverse the First Part**: Reverse the first `k` elements to correctly place them in the rotated array.
4. **Reverse the Remaining Part**: Reverse the remaining elements (from index `k` to the end) to finalize the rotation.

# Complexity

- **Time Complexity**:
  - Reversing the entire array takes `O(n)`.
  - Reversing parts of the array also takes `O(k)` and `O(n - k) `, which together still result in `O(n)`.
  - Overall: `O(n)`.

- **Space Complexity**:
  - The rotation is performed in-place, so no extra space is used beyond the input array.
  - Overall: `O(1)`.

# Code

```cpp []
class Solution {
public:
    void rotate(std::vector<int>& nums, int k) {
        int n = nums.size();
        k = k % n; 
        reverse(nums.begin(), nums.end());
        reverse(nums.begin(), nums.begin() + k);
        reverse(nums.begin() + k, nums.end());
    }
};
```

```python []
class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        n = len(nums)
        k = k % n
        if k == 0: return
        
        nums.reverse()
        nums[:k] = reversed(nums[:k])
        nums[k:] = reversed(nums[k:])
```

```java []
class Solution {
    public void rotate(int[] nums, int k) {
        int n = nums.length;
        k = k % n;
        if (k == 0) return;
        
        reverse(nums, 0, n - 1);
        reverse(nums, 0, k - 1);
        reverse(nums, k, n - 1);
    }

    private void reverse(int[] nums, int start, int end) {
        while (start < end) {
            int temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
            start++;
            end--;
        }
    }
}
```

```javascript []
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {void} Do not return anything, modify nums in-place instead.
 */
var rotate = function (nums, k) {
    const n = nums.length;
    k = k % n;
    if (k === 0) return;

    reverse(nums, 0, n - 1);
    reverse(nums, 0, k - 1);
    reverse(nums, k, n - 1);
};

function reverse(nums, start, end) {
    while (start < end) {
        let temp = nums[start];
        nums[start] = nums[end];
        nums[end] = temp;
        start++;
        end--;
    }
}
```
```
✨ AN UPVOTE WILL BE APPRECIATED ^_~ ✨
```
['Python', 'C++', 'Java', 'Python3', 'JavaScript']