Bird
0
0
DSA Cprogramming~20 mins

Array Rotation Techniques in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Rotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Left Rotation by 2
What is the output of the array after performing a left rotation by 2 positions?
DSA C
int arr[] = {10, 20, 30, 40, 50};
int n = 5, d = 2;
// Left rotate by d
int rotated[5];
for(int i = 0; i < n; i++) {
    rotated[i] = arr[(i + d) % n];
}
for(int i = 0; i < n; i++) {
    printf("%d ", rotated[i]);
}
A20 30 40 50 10
B40 50 10 20 30
C30 40 50 10 20
D50 10 20 30 40
Attempts:
2 left
💡 Hint
Think about moving the first two elements to the end.
Predict Output
intermediate
2:00remaining
Output of Right Rotation by 3
What is the output of the array after performing a right rotation 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++) {
    printf("%d ", rotated[i]);
}
A6 1 2 3 4 5
B4 5 6 1 2 3
C3 4 5 6 1 2
D5 6 1 2 3 4
Attempts:
2 left
💡 Hint
Right rotation by 3 moves last 3 elements to the front.
🔧 Debug
advanced
2:00remaining
Identify the Error in Rotation Code
What error will this code produce when trying to left rotate an array 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];
}
for(int i = 0; i < n; i++) {
    printf("%d ", rotated[i]);
}
ANo error, output: 8 9 10 7
BArray index out of bounds error
COutput: 7 8 9 10
DCompilation error due to invalid syntax
Attempts:
2 left
💡 Hint
Consider how modulo operator handles d greater than n.
Predict Output
advanced
2:00remaining
Output of Reversal Algorithm for Left Rotation
What is the output of the array after left rotating by 3 using the reversal algorithm?
DSA C
void reverse(int arr[], int start, int end) {
    while(start < end) {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
}

int arr[] = {1, 2, 3, 4, 5, 6, 7};
int n = 7, d = 3;
reverse(arr, 0, d - 1);
reverse(arr, d, n - 1);
reverse(arr, 0, n - 1);
for(int i = 0; i < n; i++) {
    printf("%d ", arr[i]);
}
A4 5 6 7 1 2 3
B7 6 5 4 3 2 1
C5 6 7 1 2 3 4
D3 2 1 7 6 5 4
Attempts:
2 left
💡 Hint
Reversal algorithm reverses parts then whole array.
🧠 Conceptual
expert
2:00remaining
Minimum Number of Rotations to Restore Original Array
Given an array of length n, what is the minimum number of left rotations needed to restore the array to its original order?
A0
B1
Cn - 1
Dn
Attempts:
2 left
💡 Hint
Think about how many times you must rotate to get back to start.