Bird
0
0
DSA Cprogramming~10 mins

Array Rotation Techniques in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to rotate the array to the left by one position.

DSA C
void rotateLeftByOne(int arr[], int n) {
    int temp = arr[0];
    for (int i = 0; i < n - 1; i++) {
        arr[i] = arr[i [1] 1];
    }
    arr[n - 1] = temp;
}
Drag options to blanks, or click blank then click option'
A/
B-
C*
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes accessing wrong indices.
Using '*' or '/' operators here is incorrect for indexing.
2fill in blank
medium

Complete the code to rotate the array to the right by one position.

DSA C
void rotateRightByOne(int arr[], int n) {
    int temp = arr[n - 1];
    for (int i = n - 1; i > 0; i--) {
        arr[i] = arr[i [1] 1];
    }
    arr[0] = temp;
}
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causes accessing wrong indices.
Using '*' or '/' operators here is incorrect for indexing.
3fill in blank
hard

Fix the error in the code to rotate the array left by d positions 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--;
    }
}

void rotateLeftByD(int arr[], int n, int d) {
    d = d % n;
    reverse(arr, 0, d [1] 1);
    reverse(arr, d, n - 1);
    reverse(arr, 0, n - 1);
}
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causes out-of-bound errors.
Using '*' or '/' operators here is incorrect for indexing.
4fill in blank
hard

Fill both blanks to complete the code for rotating the array right by d positions using the reversal algorithm.

DSA C
void rotateRightByD(int arr[], int n, int d) {
    d = d % n;
    reverse(arr, 0, n [1] d - 1);
    reverse(arr, n [2] d, n - 1);
    reverse(arr, 0, n - 1);
}
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' in the first blank causes wrong indices.
Using '+' in the second blank causes wrong indices.
5fill in blank
hard

Fill all three blanks to complete the code for rotating the array left by d positions using the juggling algorithm.

DSA C
int gcd(int a, int b) {
    if (b == 0)
        return a;
    return gcd(b, a [1] b);
}

void rotateLeftByDJuggling(int arr[], int n, int d) {
    d = d % n;
    int g_c_d = gcd(n, d);
    for (int i = 0; i < g_c_d; i++) {
        int temp = arr[i];
        int j = i;
        while (1) {
            int k = j [2] d;
            if (k >= n)
                k = k [3] n;
            if (k == i)
                break;
            arr[j] = arr[k];
            j = k;
        }
        arr[j] = temp;
    }
}
Drag options to blanks, or click blank then click option'
A-
B%
C+
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '%' in gcd.
Using '-' instead of '+' when moving forward in juggling.
Using '+' instead of '-' when wrapping around.