Good DSA problem -ROTATE ARRAY
Ankush Chudiwal
Posted on November 23, 2024
Explanation of Code: Rotating an Array by d
Elements (Counter-Clockwise)
The code provides a solution to rotate an array arr
by d
elements in a counter-clockwise direction using the reversal algorithm.
Code Walkthrough
class Solution {
// Function to rotate an array by d elements in counter-clockwise direction.
static void rotateArr(int arr[], int d) {
int n = arr.length;
// Edge case: If the array has only one element or no rotation needed
if (n <= 1 || d <= 0) {
return;
}
// Normalize d in case it's greater than or equal to array length
d = d % n;
// Step 1: Reverse the first d elements
reverse(arr, 0, d - 1);
// Step 2: Reverse the remaining elements
reverse(arr, d, n - 1);
// Step 3: Reverse the entire array
reverse(arr, 0, n - 1);
}
// Helper function to reverse elements in the array
private static void reverse(int arr[], int start, int end) {
while (start < end) {
// Swap elements at start and end
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
}
Step-by-Step Explanation
1. Handle Edge Cases
- If the array length is
1
or less (n <= 1
), or if no rotation is needed (d <= 0
), the function exits early. - No modifications are performed on such inputs.
2. Normalize the Rotation Count
- If
d
is greater than or equal to the array length (d >= n
), rotating byd
elements is equivalent to rotating byd % n
. - This ensures the rotation count remains within bounds.
3. Reverse Algorithm for Rotation
The reversal algorithm rotates the array in three steps:
-
Reverse the first
d
elements- Example: For an array
[1, 2, 3, 4, 5]
andd = 2
, reverse[1, 2]
to get[2, 1, 3, 4, 5]
.
- Example: For an array
-
Reverse the remaining
n - d
elements- Reverse
[3, 4, 5]
to get[2, 1, 5, 4, 3]
.
- Reverse
-
Reverse the entire array
- Reverse
[2, 1, 5, 4, 3]
to get[3, 4, 5, 1, 2]
.
- Reverse
4. Helper Function: reverse
- The
reverse
function swaps elements in the array between the specifiedstart
andend
indices. - It iteratively swaps elements until the pointers meet.
Complexity Analysis
-
Time Complexity:
- Reversing subsets of the array involves traversing all elements exactly once.
- Total time complexity: O(n), where
n
is the length of the array.
-
Space Complexity:
- The algorithm uses constant extra space for swapping elements.
- Space complexity: O(1).
Example Walkthrough
Input:
arr = [1, 2, 3, 4, 5]
d = 2
Execution:
- Normalize
d
:d = 2 % 5 = 2
- Reverse the first
d
elements:[1, 2]
→[2, 1]
- Array becomes:
[2, 1, 3, 4, 5]
- Array becomes:
- Reverse the remaining elements:
[3, 4, 5]
→[5, 4, 3]
- Array becomes:
[2, 1, 5, 4, 3]
- Array becomes:
- Reverse the entire array:
[2, 1, 5, 4, 3]
→[3, 4, 5, 1, 2]
Output:
[3, 4, 5, 1, 2]
Advantages
- The reversal algorithm is efficient and does not require additional space.
- It provides a systematic approach to solving array rotation problems.
💖 💪 🙅 🚩
Ankush Chudiwal
Posted on November 23, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.