|
| 1 | +--- |
| 2 | +id: rotate-array |
| 3 | +title: Rotate Array |
| 4 | +sidebar_label: 0021 - Rotate Array |
| 5 | +tags: |
| 6 | + - Easy |
| 7 | + - Arrays |
| 8 | + - Rotation |
| 9 | + - GeeksforGeeks |
| 10 | + - CPP |
| 11 | + - DSA |
| 12 | +description: "This tutorial covers the solution to the Rotate Array problem from the GeeksforGeeks website, featuring implementations in C++." |
| 13 | +--- |
| 14 | +## Problem Description |
| 15 | + |
| 16 | +Given an unsorted array `arr[]` of size `n`. Rotate the array to the left (counter-clockwise direction) by `d` steps, where `d` is a positive integer. |
| 17 | + |
| 18 | +## Examples |
| 19 | + |
| 20 | +**Example 1:** |
| 21 | + |
| 22 | +``` |
| 23 | +Input: |
| 24 | +n = 5, d = 2 |
| 25 | +arr = [1, 2, 3, 4, 5] |
| 26 | +Output: [3, 4, 5, 1, 2] |
| 27 | +Explanation: After rotating the array by 2 positions to the left, the array becomes [3, 4, 5, 1, 2]. |
| 28 | +``` |
| 29 | + |
| 30 | +**Example 2:** |
| 31 | + |
| 32 | +``` |
| 33 | +Input: |
| 34 | +n = 7, d = 3 |
| 35 | +arr = [2, 4, 6, 8, 10, 12, 14] |
| 36 | +Output: [8, 10, 12, 14, 2, 4, 6] |
| 37 | +Explanation: After rotating the array by 3 positions to the left, the array becomes [8, 10, 12, 14, 2, 4, 6]. |
| 38 | +``` |
| 39 | + |
| 40 | +## Your Task |
| 41 | + |
| 42 | +You don't need to read input or print anything. Your task is to complete the function `rotateArr()` which takes the array `arr`, integer `d` and integer `n`, and rotates the array by `d` elements to the left. |
| 43 | + |
| 44 | +Expected Time Complexity: $O(n)$ |
| 45 | + |
| 46 | +Expected Auxiliary Space: $O(1)$ |
| 47 | + |
| 48 | +## Constraints |
| 49 | + |
| 50 | +* `1 ≤ n ≤ 10^7` |
| 51 | +* `1 ≤ d ≤ n` |
| 52 | +* `0 ≤ arr[i] ≤ 10^5` |
| 53 | + |
| 54 | +## Problem Explanation |
| 55 | + |
| 56 | +The task is to rotate an array to the left by `d` elements. Rotating an array means shifting all elements to the left by a given number of positions, and the elements that fall off the start of the array reappear at the end. |
| 57 | + |
| 58 | +## Code Implementation |
| 59 | + |
| 60 | +### C++ Solution |
| 61 | + |
| 62 | +```cpp |
| 63 | +//{ Driver Code Starts |
| 64 | +#include<bits/stdc++.h> |
| 65 | +using namespace std; |
| 66 | + |
| 67 | +// } Driver Code Ends |
| 68 | +class Solution{ |
| 69 | + public: |
| 70 | + //Function to rotate an array by d elements in counter-clockwise direction. |
| 71 | + void rotateArr(int arr[], int d, int n){ |
| 72 | + // Edge case: if d is 0 or d equals n, array remains unchanged |
| 73 | + if(d == 0 || d == n) |
| 74 | + return; |
| 75 | + |
| 76 | + // Normalize d if it's greater than n |
| 77 | + d = d % n; |
| 78 | + |
| 79 | + // Reverse the first d elements |
| 80 | + reverse(arr, arr + d); |
| 81 | + |
| 82 | + // Reverse the remaining n-d elements |
| 83 | + reverse(arr + d, arr + n); |
| 84 | + |
| 85 | + // Reverse the entire array |
| 86 | + reverse(arr, arr + n); |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +//{ Driver Code Starts. |
| 91 | + |
| 92 | +int main() { |
| 93 | + int t; |
| 94 | + //taking testcases |
| 95 | + cin >> t; |
| 96 | + |
| 97 | + while(t--){ |
| 98 | + int n, d; |
| 99 | + |
| 100 | + //input n and d |
| 101 | + cin >> n >> d; |
| 102 | + |
| 103 | + int arr[n]; |
| 104 | + |
| 105 | + //inserting elements in the array |
| 106 | + for(int i = 0; i < n; i++){ |
| 107 | + cin >> arr[i]; |
| 108 | + } |
| 109 | + Solution ob; |
| 110 | + //calling rotateArr() function |
| 111 | + ob.rotateArr(arr, d, n); |
| 112 | + |
| 113 | + //printing the elements of the array |
| 114 | + for(int i =0; i < n; i++){ |
| 115 | + cout << arr[i] << " "; |
| 116 | + } |
| 117 | + cout << endl; |
| 118 | + } |
| 119 | + return 0; |
| 120 | +} |
| 121 | +// } Driver Code Ends |
| 122 | +``` |
| 123 | +
|
| 124 | +## Example Walkthrough |
| 125 | +
|
| 126 | +**Example 1:** |
| 127 | +
|
| 128 | +For the input: |
| 129 | +``` |
| 130 | +n = 5, d = 2 |
| 131 | +arr = [1, 2, 3, 4, 5] |
| 132 | +``` |
| 133 | +1. Rotate the first `d` elements: [2, 1, 3, 4, 5] |
| 134 | +2. Rotate the remaining `n-d` elements: [2, 1, 5, 4, 3] |
| 135 | +3. Rotate the entire array: [3, 4, 5, 1, 2] |
| 136 | +
|
| 137 | +**Example 2:** |
| 138 | +
|
| 139 | +For the input: |
| 140 | +``` |
| 141 | +n = 7, d = 3 |
| 142 | +arr = [2, 4, 6, 8, 10, 12, 14] |
| 143 | +``` |
| 144 | +1. Rotate the first `d` elements: [6, 4, 2, 8, 10, 12, 14] |
| 145 | +2. Rotate the remaining `n-d` elements: [6, 4, 2, 14, 12, 10, 8] |
| 146 | +3. Rotate the entire array: [8, 10, 12, 14, 2, 4, 6] |
| 147 | +
|
| 148 | +## Solution Logic: |
| 149 | +
|
| 150 | +1. Use three reverse operations: |
| 151 | + - Reverse the first `d` elements. |
| 152 | + - Reverse the remaining `n-d` elements. |
| 153 | + - Reverse the entire array. |
| 154 | +2. This approach ensures that the elements are shifted in place with an $O(n)$ time complexity and $O(1)$ auxiliary space. |
| 155 | +
|
| 156 | +## Time Complexity |
| 157 | +
|
| 158 | +* The time complexity is $O(n)$ as the array is processed three times. |
| 159 | +
|
| 160 | +## Space Complexity |
| 161 | +
|
| 162 | +* The auxiliary space complexity is $O(1)$ as no additional storage is used. |
0 commit comments