You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given an integer array `nums`, rotate the array to the right by `n` steps, where `n` is non-negative number.
2
+
Given an integer array `nums`, rotate the array to the right by `n` steps, where `n` is a non-negative number. The rotation is performed **in-place**, meaning no additional array is used for the operation.
3
3
4
4
### Examples
5
5
Example 1:
6
6
7
7
Input: nums = [1,2,3,4,5,6,7], n = 4
8
-
Output: [5,6,7,1,2,3,4]
8
+
Output: [4,5,6,7,1,2,3]
9
9
10
10
Example 2:
11
11
12
12
Input: nums = [-10, 4, 5, -1], n = 2
13
13
Output: [5, -1, -10, 4]
14
14
15
-
**Algorithmic Steps:**
16
-
This problem is solved with the help of **two pointers**technique, which is used to reverse subarrays. The algorithmic approach can be summarized as follows:
15
+
**Algorithm overview:**
16
+
This algorithm leverages the **reversal technique**(a two-pointer approach) to efficiently rotate the array in-place.
17
17
18
-
1. Initialize `length` variable to store length of an input array.
18
+
To rotate the array to the right by `n` steps:
19
19
20
-
2. Update the number of times rotation(i.e, `n`) to `n % length` incase of number of rotations greater than length of an array.
20
+
1. Reverse the entire array.
21
+
2. Reverse the first `n` elements.
22
+
3. Reverse the remaining `length - n` elements.
21
23
22
-
3. Create a helper function to reverse elements of an array using swap operation with the help left and right pointers(i.e, `left` and `right`). The left pointer needs to be incremented and right pointer needs to decremented for each iteration.
24
+
#### Detailed steps:
23
25
24
-
4. Invoke reversal operation for entire array of elements(i.e, from the beginning to end of an array).
26
+
1.**Calculate Array Length**
27
+
Initialize a variable `length` to store the total number of elements in the array.
25
28
26
-
5. Invoke reversal operation for first `n` number of elements(i.e, from the beginning to n-1).
29
+
2.**Edge case**
30
+
No rotation is required when the given array is empty and number of rotations is equals to the length of an array. This condition is helpful for early exit.
31
+
32
+
3.**Normalize the Rotation Count**
33
+
Update the rotation count by taking `n % length` to handle cases where `n` exceeds the array size. This ensures we perform only the necessary rotations.
27
34
28
-
6. Invoke reversal operation for remaining number of elements(i.e, from `n-1` to end of an array).
35
+
4.**Define a Reversal Helper Function**
36
+
Implement a helper function that reverses elements between two indices (`left` and `right`) in the array using a two-pointer technique. Swap the elements at `left` and `right`, then increment `left` and decrement `right` in each iteration until both pointers meet.
29
37
30
-
7. Return in-place rotated array as output.
38
+
5.**Reverse the Entire Array**
39
+
Apply the reversal function to the full array (from index `0` to `length - 1`). This step brings the elements that need to be rotated to the front, but in reverse order.
31
40
32
-
**Time and Space complexity:**
41
+
6.**Reverse the First `n` Elements**
42
+
Reverse the first `n` elements (from index `0` to `n - 1`) to restore them to the correct order.
33
43
34
-
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements. This is because we are performing three reversal operations on subarrays and each reversal operation takes `O(n)`. So the overall time complexity is `O(n)`.
44
+
7.**Reverse the Remaining Elements**
45
+
Reverse the remaining elements (from index `n` to `length - 1`) to place the original beginning of the array back in the correct order.
35
46
36
-
Here, we don't use any additional datastructure due to in-place rotation. Hence, the space complexity will be `O(1)`.
47
+
8.**Return the Rotated Array**
48
+
The array has now been successfully rotated in-place and is ready for use.
49
+
50
+
## Time and Space Complexity
51
+
52
+
-**Time Complexity:**`O(n)`
53
+
The algorithm performs three reverse operations, each taking `O(n)` time in total.
54
+
55
+
-**Space Complexity:**`O(1)`
56
+
The rotation is done in-place, using only a constant amount of extra space regardless of input size.
0 commit comments