📖 Scenario: You are helping a friend organize their collection of unique stickers. They want to see all possible groups of stickers they can create from their collection.
🎯 Goal: Build a program that generates all possible subsets (groups) of a given list of stickers using the bitmask technique.
📋 What You'll Learn
Create an array called stickers with the exact values ['star', 'heart', 'smile'].
Create a variable called n that stores the length of the stickers array.
Use a for loop with variable bitmask from 0 to (1 << n) - 1 to generate subsets.
Inside the loop, use another for loop with variable j from 0 to n - 1 to check bits.
Create an array called subset to hold the current subset stickers.
Print the array allSubsets containing all subsets after generation.
💡 Why This Matters
🌍 Real World
Generating all combinations of items is useful in planning, testing, and decision making.
💼 Career
Understanding subsets and bitmasking helps in coding interviews and solving combinatorial problems efficiently.
Progress0 / 4 steps
1
Create the stickers array
Create an array called stickers with these exact values: ['star', 'heart', 'smile'].
DSA Typescript
Hint
Use const stickers = ['star', 'heart', 'smile']; to create the array.
2
Create the length variable
Create a variable called n that stores the length of the stickers array.
DSA Typescript
Hint
Use const n = stickers.length; to get the number of stickers.
3
Generate all subsets using bitmask
Create an empty array called allSubsets. Use a for loop with variable bitmask from 0 to (1 << n) - 1. Inside it, create an empty array called subset. Use another for loop with variable j from 0 to n - 1. If the bit at position j in bitmask is set, add stickers[j] to subset. After the inner loop, add subset to allSubsets.
DSA Typescript
Hint
Use two nested loops and bitwise AND to check bits. Add subsets to allSubsets.
4
Print all subsets
Print the array allSubsets to show all generated subsets.
DSA Typescript
Hint
Use console.log(allSubsets); to print the result.
Practice
(1/5)
1. What does the bitmask technique help generate when applied to a set of elements?
easy
A. Only the largest subset
B. Only subsets with odd number of elements
C. Only subsets with even number of elements
D. All possible subsets of the set
Solution
Step 1: Understand bitmask representation
Each bit in the bitmask represents whether an element is included (1) or excluded (0) in a subset.
Step 2: Generate subsets using bitmask
By iterating through all bitmasks from 0 to 2^n - 1, we cover all combinations of elements, thus all subsets.
Final Answer:
All possible subsets of the set -> Option D
Quick Check:
Bitmask = All subsets [OK]
Hint: Bitmask from 0 to 2^n-1 covers all subsets [OK]
Common Mistakes:
Thinking bitmask generates only even or odd subsets
Confusing subsets with permutations
Assuming bitmask generates only largest subsets
2. Which of the following TypeScript code snippets correctly initializes the bitmask loop for generating subsets of an array arr of length n?
easy
A. for (let mask = 0; mask <= n; mask++) {}
B. for (let mask = 1; mask <= n; mask++) {}
C. for (let mask = 0; mask < (1 << n); mask++) {}
D. for (let mask = 0; mask < n; mask++) {}
Solution
Step 1: Understand bitmask range
Bitmask must cover all numbers from 0 to 2^n - 1, which is (1 << n) in code.
Step 2: Check loop correctness
Loop from 0 to less than (1 << n) correctly iterates all bitmasks representing subsets.
Final Answer:
for (let mask = 0; mask < (1 << n); mask++) {} -> Option C
Quick Check:
Loop 0 to 2^n-1 = for (let mask = 0; mask < (1 << n); mask++) {} [OK]
Hint: Use (1 << n) for total subsets count [OK]
Common Mistakes:
Using mask <= n instead of mask < 2^n
Starting loop from 1 misses empty subset
Using mask < n only covers single elements
3. What is the output of the following TypeScript code?
const arr = ["a", "b", "c"];
const subsets: string[][] = [];
const n = arr.length;
for (let mask = 0; mask < (1 << n); mask++) {
const subset: string[] = [];
for (let i = 0; i < n; i++) {
if ((mask & (1 << i)) !== 0) {
subset.push(arr[i]);
}
}
subsets.push(subset);
}
console.log(subsets);
medium
A. [["a"], ["b", "a"], ["c", "b"], ["a", "b", "c"]]