Bird
0
0
DSA Cprogramming~20 mins

Subsets Generation Using Bitmask in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bitmask Subsets Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of subsets generated by bitmask
What is the output of the following C code that generates all subsets of the array [1, 2, 3] using bitmask?
DSA C
#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3};
    int n = 3;
    for (int mask = 0; mask < (1 << n); mask++) {
        printf("{");
        for (int i = 0; i < n; i++) {
            if (mask & (1 << i)) {
                printf("%d", arr[i]);
                if (i != n - 1) {
                    int j = i + 1;
                    while (j < n && !(mask & (1 << j))) j++;
                    if (j < n) printf(", ");
                }
            }
        }
        printf("}\n");
    }
    return 0;
}
A
{}
{3}
{2}
{1}
{3, 2}
{3, 1}
{2, 1}
{3, 2, 1}
B
{}
{1}
{2}
{3}
{1, 2}
{1, 3}
{2, 3}
{1, 2, 3}
C
{}
{1}
{3}
{2}
{1, 3}
{1, 2}
{3, 2}
{1, 3, 2}
D
{}
{1}
{2}
{1, 2}
{3}
{1, 3}
{2, 3}
{1, 2, 3}
Attempts:
2 left
💡 Hint
Remember that bitmask from 0 to 2^n - 1 represents subsets in increasing order.
🧠 Conceptual
intermediate
1:00remaining
Number of subsets generated by bitmask for n elements
If you use bitmasking to generate all subsets of a set with n elements, how many subsets will be generated?
An!
B2^n
Cn^2
Dn
Attempts:
2 left
💡 Hint
Each element can be either included or excluded independently.
🔧 Debug
advanced
1:30remaining
Identify the error in subset generation code
What error does the following C code produce when generating subsets using bitmask for array [1, 2, 3]? #include int main() { int arr[] = {1, 2, 3}; int n = 3; for (int mask = 0; mask <= (1 << n); mask++) { printf("{"); for (int i = 0; i < n; i++) { if (mask & (1 << i)) { printf("%d", arr[i]); } } printf("}\n"); } return 0; }
ARuns but prints one extra empty subset at the end
BRuns correctly and prints all subsets
CRuns but causes an infinite loop
DCauses a runtime error due to out-of-bounds access
Attempts:
2 left
💡 Hint
Check the loop condition for mask variable.
🚀 Application
advanced
2:00remaining
Find subsets with sum equal to target using bitmask
Given array [2, 4, 6, 10] and target sum 16, which subsets are printed by the following code snippet? #include int main() { int arr[] = {2, 4, 6, 10}; int n = 4; int target = 16; for (int mask = 0; mask < (1 << n); mask++) { int sum = 0; for (int i = 0; i < n; i++) { if (mask & (1 << i)) { sum += arr[i]; } } if (sum == target) { printf("{"); int first = 1; for (int i = 0; i < n; i++) { if (mask & (1 << i)) { if (!first) printf(", "); printf("%d", arr[i]); first = 0; } } printf("}\n"); } } return 0; }
A
{2, 4, 10}
{6, 10}
B
{2, 6, 10}
{4, 6, 10}
C{4, 6, 10}
D{2, 4, 6, 10}
Attempts:
2 left
💡 Hint
Calculate sums of subsets and check which equal 16.
Predict Output
expert
2:30remaining
Output of bitmask subset generation with duplicate elements
What is the output of the following C code that generates all subsets of the array [1, 2, 2] using bitmask?
DSA C
#include <stdio.h>

int main() {
    int arr[] = {1, 2, 2};
    int n = 3;
    for (int mask = 0; mask < (1 << n); mask++) {
        printf("{");
        int first = 1;
        for (int i = 0; i < n; i++) {
            if (mask & (1 << i)) {
                if (!first) printf(", ");
                printf("%d", arr[i]);
                first = 0;
            }
        }
        printf("}\n");
    }
    return 0;
}
A
{}
{1}
{2}
{2}
{2, 2}
{1, 2}
{1, 2}
{1, 2, 2}
B
{}
{1}
{2}
{1, 2}
{2}
{2, 2}
{1, 2}
{1, 2, 2}
C
{}
{1}
{2}
{1, 2}
{2}
{1, 2}
{2, 2}
{1, 2, 2}
D
{}
{1}
{2}
{2}
{1, 2}
{1, 2}
{2, 2}
{1, 2, 2}
Attempts:
2 left
💡 Hint
Bitmask order is from 0 to 7, each bit representing element inclusion.