Challenge - 5 Problems
Array Rotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Left Rotation by 2
What is the output of the following C++ code after rotating the array to the left by 2 positions?
DSA C++
int arr[] = {10, 20, 30, 40, 50}; int n = 5, d = 2; int rotated[5]; for(int i = 0; i < n; i++) { rotated[i] = arr[(i + d) % n]; } for(int i = 0; i < n; i++) { std::cout << rotated[i] << " "; }
Attempts:
2 left
💡 Hint
Remember that left rotation by d means elements shift to the left by d positions, wrapping around.
✗ Incorrect
The code shifts each element to the left by 2 positions. So element at index 0 moves to index (0+2)%5=2, which is 30, and so on.
❓ Predict Output
intermediateOutput of Right Rotation by 3
What is the output of the following C++ code after rotating the array to the right by 3 positions?
DSA C++
int arr[] = {1, 2, 3, 4, 5, 6}; int n = 6, d = 3; int rotated[6]; for(int i = 0; i < n; i++) { rotated[(i + d) % n] = arr[i]; } for(int i = 0; i < n; i++) { std::cout << rotated[i] << " "; }
Attempts:
2 left
💡 Hint
Right rotation by d means elements move to the right by d positions, wrapping around.
✗ Incorrect
Each element is placed at index (i + d) % n. So element 1 at index 0 moves to index 3, element 4 at index 3 moves to index 0, etc.
🔧 Debug
advancedIdentify the Error in Rotation Code
What error will the following C++ code produce when trying to rotate an array left by d positions?
DSA C++
int arr[] = {7, 8, 9, 10}; int n = 4, d = 5; int rotated[4]; for(int i = 0; i < n; i++) { rotated[i] = arr[(i - d + n) % n]; } for(int i = 0; i < n; i++) { std::cout << rotated[i] << " "; }
Attempts:
2 left
💡 Hint
Check how modulo operator behaves with negative numbers in C++.
✗ Incorrect
In C++, modulo with negative numbers can produce negative results, causing invalid negative array indices and runtime errors. Adding n before modulo fixes this.
🧠 Conceptual
advancedMinimum Rotations to Restore Original Array
Given an array of length 7, if you rotate it left by 3 positions twice in a row, how many total left rotations are needed to restore the array to its original order?
Attempts:
2 left
💡 Hint
Think about how rotations add up modulo the array length.
✗ Incorrect
Rotating left by 3 twice equals rotating left by 6. To restore original, rotate left by 1 more (total 7) because 7 % 7 = 0.
🚀 Application
expertOutput After Multiple Rotations
What is the output of the following C++ code after performing the described rotations?
DSA C++
int arr[] = {2, 4, 6, 8, 10}; int n = 5; int temp; // Rotate left by 1 for(int i = 0; i < n - 1; i++) { std::swap(arr[i], arr[i+1]); } // Rotate right by 2 for(int i = 0; i < 2; i++) { temp = arr[n-1]; for(int j = n-1; j > 0; j--) { arr[j] = arr[j-1]; } arr[0] = temp; } for(int i = 0; i < n; i++) { std::cout << arr[i] << " "; }
Attempts:
2 left
💡 Hint
Perform each rotation step carefully and track the array state.
✗ Incorrect
Left rotation by 1 using swaps results in {4,6,8,10,2}. Then right rotation by 2 results in {10,2,4,6,8}.
