|
| 1 | +--- |
| 2 | +id: rotate-array |
| 3 | +title: Rotate Array |
| 4 | +sidebar_label: 0189 Rotate Array |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - LeetCode |
| 8 | + - Java |
| 9 | + - Python |
| 10 | + - C++ |
| 11 | +description: "This is a solution to the Rotate Array problem on LeetCode." |
| 12 | +--- |
| 13 | + |
| 14 | +## Problem Description |
| 15 | + |
| 16 | +Given an integer array nums, rotate the array to the right by k steps, where k is non-negative. |
| 17 | + |
| 18 | +### Examples |
| 19 | + |
| 20 | +**Example 1:** |
| 21 | + |
| 22 | +``` |
| 23 | +Input: nums = [1,2,3,4,5,6,7], k = 3 |
| 24 | +Output: [5,6,7,1,2,3,4] |
| 25 | +Explanation: |
| 26 | +rotate 1 steps to the right: [7,1,2,3,4,5,6] |
| 27 | +rotate 2 steps to the right: [6,7,1,2,3,4,5] |
| 28 | +rotate 3 steps to the right: [5,6,7,1,2,3,4] |
| 29 | +
|
| 30 | +``` |
| 31 | + |
| 32 | +**Example 2:** |
| 33 | + |
| 34 | +``` |
| 35 | +Input: nums = [-1,-100,3,99], k = 2 |
| 36 | +Output: [3,99,-1,-100] |
| 37 | +Explanation: |
| 38 | +rotate 1 steps to the right: [99,-1,-100,3] |
| 39 | +rotate 2 steps to the right: [3,99,-1,-100] |
| 40 | +``` |
| 41 | + |
| 42 | +### Constraints |
| 43 | + |
| 44 | +- $1 <= nums.length <= 105$ |
| 45 | +- $-231 <= nums[i] <= 231 - 1$ |
| 46 | +- $0 <= k <= 105$ |
| 47 | + |
| 48 | + |
| 49 | +## Solution for Candy Distribution Problem |
| 50 | + |
| 51 | +### Intuition And Approach |
| 52 | + |
| 53 | +To rotate an array to the right by k steps, we need to move the last k elements to the front and shift the rest of the elements to the right. A straightforward way to achieve this in-place (without using extra space for another array) is to use the reversal method. |
| 54 | + |
| 55 | +Here is the step-by-step approach: |
| 56 | + |
| 57 | +1. Adjust k: |
| 58 | + |
| 59 | +If k is greater than the length of the array, rotating by k steps is the same as rotating by k % n steps (where n is the length of the array). This is because rotating by the length of the array brings it back to the original position. |
| 60 | +Calculate k = k % n. |
| 61 | + |
| 62 | +2. Reverse the entire array: |
| 63 | + |
| 64 | +By reversing the entire array, the last k elements (which we want to move to the front) will be at the beginning, but in reverse order. |
| 65 | +For example, reversing [1, 2, 3, 4, 5, 6, 7] gives [7, 6, 5, 4, 3, 2, 1]. |
| 66 | + |
| 67 | +3. Reverse the first k elements: |
| 68 | + |
| 69 | +The first k elements are now the elements that were originally at the end of the array. Reverse these to restore their original order. |
| 70 | +Continuing the example, reversing the first 3 elements of [7, 6, 5, 4, 3, 2, 1] gives [5, 6, 7, 4, 3, 2, 1]. |
| 71 | + |
| 72 | +4. Reverse the remaining n - k elements: |
| 73 | + |
| 74 | +Finally, reverse the rest of the array (from the k-th element to the end) to restore their order. |
| 75 | +In the example, reversing the elements from index 3 to 6 of [5, 6, 7, 4, 3, 2, 1] gives [5, 6, 7, 1, 2, 3, 4]. |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +#### Code in Different Languages |
| 80 | + |
| 81 | +<Tabs> |
| 82 | + <TabItem value="Java" label="Java"> |
| 83 | + <SolutionAuthor name="@mahek0620"/> |
| 84 | + ```java |
| 85 | + |
| 86 | + class Solution { |
| 87 | + public void rotate(int[] nums, int k) { |
| 88 | + // Ensure k is within the bounds of the array length |
| 89 | + k = k % nums.length; |
| 90 | + |
| 91 | + // Reverse the entire array |
| 92 | + reverse(nums, 0, nums.length - 1); |
| 93 | + |
| 94 | + // Reverse the first k elements |
| 95 | + reverse(nums, 0, k - 1); |
| 96 | + |
| 97 | + // Reverse the remaining elements |
| 98 | + reverse(nums, k, nums.length - 1); |
| 99 | + } |
| 100 | + |
| 101 | + // Helper function to reverse elements in the array from start to end |
| 102 | + private void reverse(int[] nums, int start, int end) { |
| 103 | + while (start < end) { |
| 104 | + int temp = nums[start]; |
| 105 | + nums[start] = nums[end]; |
| 106 | + nums[end] = temp; |
| 107 | + start++; |
| 108 | + end--; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + ``` |
| 113 | + </TabItem> |
| 114 | + <TabItem value="Python" label="Python"> |
| 115 | + <SolutionAuthor name="@mahek0620"/> |
| 116 | + ```python |
| 117 | + |
| 118 | + class Solution(object): |
| 119 | + def rotate(self, nums, k): |
| 120 | + k = k % len(nums) |
| 121 | + |
| 122 | + # Reverse the entire array |
| 123 | + self.reverse(nums, 0, len(nums) - 1) |
| 124 | + |
| 125 | + # Reverse the first k elements |
| 126 | + self.reverse(nums, 0, k - 1) |
| 127 | + |
| 128 | + # Reverse the remaining elements |
| 129 | + self.reverse(nums, k, len(nums) - 1) |
| 130 | + |
| 131 | + def reverse(self, nums, start, end): |
| 132 | + while start < end: |
| 133 | + nums[start], nums[end] = nums[end], nums[start] |
| 134 | + start += 1 |
| 135 | + end -= 1 |
| 136 | + |
| 137 | + ``` |
| 138 | + </TabItem> |
| 139 | + |
| 140 | + |
| 141 | +<TabItem value="C++" label="C++"> |
| 142 | + <SolutionAuthor name="@mahek0620"/> |
| 143 | + ```cpp |
| 144 | + |
| 145 | + #include <vector> |
| 146 | + using namespace std; |
| 147 | + |
| 148 | + class Solution { |
| 149 | + public: |
| 150 | + void rotate(vector<int>& nums, int k) { |
| 151 | + // Ensure k is within the bounds of the array length |
| 152 | + k = k % nums.size(); |
| 153 | + |
| 154 | + // Reverse the entire array |
| 155 | + reverse(nums, 0, nums.size() - 1); |
| 156 | + |
| 157 | + // Reverse the first k elements |
| 158 | + reverse(nums, 0, k - 1); |
| 159 | + |
| 160 | + // Reverse the remaining elements |
| 161 | + reverse(nums, k, nums.size() - 1); |
| 162 | + } |
| 163 | + |
| 164 | + private: |
| 165 | + void reverse(vector<int>& nums, int start, int end) { |
| 166 | + while (start < end) { |
| 167 | + int temp = nums[start]; |
| 168 | + nums[start] = nums[end]; |
| 169 | + nums[end] = temp; |
| 170 | + start++; |
| 171 | + end--; |
| 172 | + } |
| 173 | + } |
| 174 | + }; |
| 175 | + ``` |
| 176 | + </TabItem> |
| 177 | +</Tabs> |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | +## References |
| 182 | + |
| 183 | +- **LeetCode Problem:** [Rotate Array Problem](https://leetcode.com/problems/rotate-array/) |
| 184 | +- **Solution Link:** [Rotate Array Solution on LeetCode](https://leetcode.com/problems/rotate-array/solutions/5273312/rotate-array-solution) |
| 185 | +- **Authors GeeksforGeeks Profile:** [Mahek Patel](https://leetcode.com/u/mahekrpatel611/) |
0 commit comments