0
0
DSA Cprogramming~10 mins

Generate All Permutations of Array 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 swap two elements in the array.

DSA C
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = [1];
}
Drag options to blanks, or click blank then click option'
Aa
Btemp
Cb
D*temp
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning *a to *b instead of using temp.
Using incorrect pointer dereferencing.
2fill in blank
medium

Complete the code to print the current permutation of the array.

DSA C
void printArray(int arr[], int size) {
    for (int i = 0; i < [1]; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}
Drag options to blanks, or click blank then click option'
A0
Barr
Ci
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using the array name instead of size in the loop condition.
Looping beyond the array bounds.
3fill in blank
hard

Fix the error in the recursive call to generate permutations.

DSA C
void permute(int arr[], int l, int r) {
    if (l == r) {
        printArray(arr, r + 1);
    } else {
        for (int i = l; i <= r; i++) {
            swap(&arr[l], &arr[i]);
            permute(arr, [1], r);
            swap(&arr[l], &arr[i]);
        }
    }
}
Drag options to blanks, or click blank then click option'
Al + 1
Bi + 1
Cr + 1
Dl
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong index in the recursive call.
Not incrementing the left index causing infinite recursion.
4fill in blank
hard

Fill both blanks to complete the for loop and swap calls correctly.

DSA C
for (int i = [1]; i <= [2]; i++) {
    swap(&arr[l], &arr[i]);
    permute(arr, l + 1, r);
    swap(&arr[l], &arr[i]);
}
Drag options to blanks, or click blank then click option'
Al
Br
C0
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop from 0 instead of l.
Looping beyond r causing out-of-bounds errors.
5fill in blank
hard

Fill all three blanks to complete the main function setup and call permutation.

DSA C
int main() {
    int arr[] = [1];
    int n = sizeof(arr) / sizeof(arr[0]);
    permute(arr, [2], [3]);
    return 0;
}
Drag options to blanks, or click blank then click option'
A{1, 2, 3}
B0
Cn - 1
D{3, 2, 1}
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect array initialization.
Passing wrong indices to permute function.