📖 Scenario: Imagine you have a small collection of unique fruits and you want to see all possible combinations of these fruits you can pick. This is like making different fruit baskets with any number of fruits from your collection.
🎯 Goal: You will write a C++ program that generates all possible subsets (combinations) of a given list of fruits using a bitmask technique.
📋 What You'll Learn
Create a vector of strings called fruits with exactly these values: "apple", "banana", "cherry"
Create an integer variable called n that stores the size of the fruits vector
Use a for loop with variable mask from 0 to (1 << n) - 1 to generate all subsets
Inside the loop, use another for loop with variable i from 0 to n - 1 to check if the i-th bit in mask is set
Print each subset on a new line with fruits separated by spaces, or print Empty if the subset has no fruits
💡 Why This Matters
🌍 Real World
Generating all combinations of items is useful in shopping apps, menu planning, and testing different feature sets.
💼 Career
Understanding subsets and bitmasking is important for coding interviews and solving problems involving combinations efficiently.
Progress0 / 4 steps
1
Create the fruits list
Create a std::vector<std::string> called fruits with these exact values: "apple", "banana", "cherry"
DSA C++
Hint
Use std::vector<std::string> and initialize it with the three fruit names inside curly braces.
2
Create the size variable
Create an integer variable called n and set it to the size of the fruits vector using fruits.size()
DSA C++
Hint
Use int n = fruits.size(); to get the number of fruits.
3
Generate all subsets using bitmask
Use a for loop with variable mask from 0 to (1 << n) - 1. Inside it, use another for loop with variable i from 0 to n - 1 to check if the i-th bit in mask is set using (mask & (1 << i)). If set, print fruits[i] followed by a space.
DSA C++
Hint
Use nested loops: outer loop for mask, inner loop for i. Use bitwise AND to check bits.
4
Print subsets or Empty if none
Modify the inner loop to print Empty if no fruits are printed for a subset. Use a boolean variable empty set to true before the inner loop and set it to false if any fruit is printed. After the inner loop, if empty is still true, print Empty. Then print a newline.
DSA C++
Hint
Use a boolean flag to track if any fruit was printed. If none, print Empty.
Practice
(1/5)
1. What does the bitmask technique help to generate when applied to a set of elements?
easy
A. Only the largest subset of the set
B. All possible subsets of the set
C. The sum of all elements in the set
D. The sorted version 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 over all bitmasks from 0 to 2^n - 1, we get all possible subsets of the set.
Final Answer:
All possible subsets of the set -> Option B
Quick Check:
Bitmask = subsets generator [OK]
Hint: Bitmask bits map directly to subset elements included [OK]
Common Mistakes:
Thinking bitmask generates only one subset
Confusing subsets with permutations
Assuming bitmask sorts elements
2. Which of the following is the correct C++ syntax to iterate over all bitmasks for a set of size n?
easy
A. for (int mask = 0; mask <= n; mask++)
B. for (int mask = 1; mask <= n; mask++)
C. for (int mask = 0; mask < (1 << n); mask++)
D. for (int mask = 0; mask < n; mask++)
Solution
Step 1: Understand bitmask range
Bitmask ranges from 0 to 2^n - 1, which is (1 << n) - 1 in C++.
Step 2: Correct loop syntax
The loop must run from 0 to less than (1 << n) to cover all subsets.
Final Answer:
for (int mask = 0; mask < (1 << n); mask++) -> Option C
Quick Check:
Loop runs 0 to 2^n-1 [OK]
Hint: Use (1 << n) for 2^n subsets count [OK]
Common Mistakes:
Using mask <= n instead of mask < (1 << n)
Starting loop from 1 instead of 0
Using mask < n which is too small
3. What is the output of the following C++ code snippet?
std::vector<int> nums = {1, 2};
for (int mask = 0; mask < (1 << 2); mask++) {
std::cout << "Subset: ";
for (int i = 0; i < 2; i++) {
if (mask & (1 << i)) std::cout << nums[i] << " ";
}
std::cout << std::endl;
}
medium
A. Subset:
Subset: 1
Subset: 2
Subset: 1 2
B. Subset: 1 2
Subset: 1
Subset: 2
Subset:
C. Subset: 1
Subset: 2
Subset: 1 2
Subset:
D. Subset:
Subset: 2
Subset: 1
Subset: 1 2
Solution
Step 1: Identify bitmask values and subsets
For n=2, masks go 0 to 3: 0 (00) no elements, 1 (01) includes nums[0]=1, 2 (10) includes nums[1]=2, 3 (11) includes nums[0]=1 and nums[1]=2.