Bird
0
0
DSA Cprogramming~10 mins

Subsets Generation Using Bitmask 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 calculate the total number of subsets for a set of size n.

DSA C
int total_subsets = 1 << [1];
Drag options to blanks, or click blank then click option'
An
Bsize
Ccount
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable name instead of n.
Using multiplication instead of bit shift.
2fill in blank
medium

Complete the for loop to iterate over all subsets represented by bitmasks.

DSA C
for (int mask = 0; mask < (1 << n); mask[1]) {
Drag options to blanks, or click blank then click option'
A+=
B--
C++
D*=
Attempts:
3 left
💡 Hint
Common Mistakes
Using decrement operator which causes an infinite loop.
Using multiplication or addition incorrectly.
3fill in blank
hard

Fix the error in the condition to check if the j-th element is included in the current subset mask.

DSA C
if (mask & (1 [1] j)) {
Drag options to blanks, or click blank then click option'
A>>
B<<
C&
D|
Attempts:
3 left
💡 Hint
Common Mistakes
Using right shift instead of left shift.
Using bitwise OR or AND incorrectly.
4fill in blank
hard

Fill both blanks to print all elements of the current subset represented by mask.

DSA C
for (int j = 0; j [1] n; j[2]) {
    if (mask & (1 << j)) {
        printf("%d ", arr[j]);
    }
}
Drag options to blanks, or click blank then click option'
A<
B<=
C++
D--
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= instead of < causing out-of-bounds access.
Using decrement operator causing infinite loop.
5fill in blank
hard

Fill all three blanks to create a function that generates all subsets of an array using bitmask.

DSA C
void generate_subsets(int arr[], int n) {
    int total = 1 [1] n;
    for (int mask = 0; mask [2] total; mask++) {
        printf("{ ");
        for (int j = 0; j [3] n; j++) {
            if (mask & (1 << j)) {
                printf("%d ", arr[j]);
            }
        }
        printf("}\n");
    }
}
Drag options to blanks, or click blank then click option'
A<<
B<
C++
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= in loop conditions causing out-of-bounds errors.
Using wrong bit shift direction.
Not incrementing loop variables properly.