> For the complete documentation index, see [llms.txt](https://coding-9.gitbook.io/untitled/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coding-9.gitbook.io/untitled/array/easy/189.-rotate-array.md).

# 189. Rotate Array

## Problem Statement

<br>

Given an integer array `nums`, rotate the array to the right by `k` steps, where `k` is non-negative.

&#x20;

**Example 1:**

<pre><code><strong>Input: nums = [1,2,3,4,5,6,7], k = 3
</strong><strong>Output: [5,6,7,1,2,3,4]
</strong><strong>Explanation:
</strong>rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]
</code></pre>

**Example 2:**

<pre><code><strong>Input: nums = [-1,-100,3,99], k = 2
</strong><strong>Output: [3,99,-1,-100]
</strong><strong>Explanation: 
</strong>rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]
</code></pre>

&#x20;

**Constraints:**

* `1 <= nums.length <= 105`
* `-231 <= nums[i] <= 231 - 1`
* `0 <= k <= 105`

## Intuition

```
Checkout this brilliant way of rotating array
 //     1 2 3 4
    //     rotate by 3 right

        
    //    reqiured ans= 2 3 4 1

    //     reverse1= 4 3 2 1
    //     reverse2= (4 3 2) 1 = 2 3 4 1
    //     reverse3= 2 3 4 (1) = 2 3 4 1   final ans
```

### Links

<https://leetcode.com/problems/rotate-array/>

### Video Links

### Approach 1:

```
Reverse 
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    void rotate(vector<int>& nums, int k) {
        k=k%nums.size();

        reverse(nums.begin(),nums.end());
        reverse(nums.begin(),nums.begin()+k);
        reverse(nums.begin()+k,nums.end());
    }
};
```

{% endcode %}

### Approach 2:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###
