Incorrect template syntax like vector subsets<int>
Trying to initialize with empty braces incorrectly
3. What is the output of the following C++ code snippet?
std::vector<int> nums = {1, 2};
std::vector subsets = {{}};
for (int num : nums) {
int n = subsets.size();
for (int i = 0; i < n; i++) {
std::vector<int> newSubset = subsets[i];
newSubset.push_back(num);
subsets.push_back(newSubset);
}
}
for (auto &subset : subsets) {
for (int x : subset) std::cout << x << " ";
std::cout << "| ";
}
medium
A. | | 1 | 2 | 1 2 |
B. | | 1 2 | 1 | 2 |
C. | 1 | 2 | 1 2 |
D. | 1 2 | 1 | 2 |
Solution
Step 1: Understand initial subsets and loop
Initially subsets = {{}}, empty subset only. For each num, we add new subsets by adding num to existing subsets.
Step 2: Trace iterations
For num=1: subsets become {}, {1}. For num=2: subsets become {}, {1}, {2}, {1, 2}.
Final Answer:
| | 1 | 2 | 1 2 | -> Option A
Quick Check:
All subsets printed with spaces and separators [OK]
Hint: Start with empty subset and add elements iteratively [OK]
Common Mistakes:
Missing the empty subset in output
Printing subsets in wrong order
Confusing push_back inside loop
4. Identify the error in this C++ code snippet for generating subsets:
std::vector<int> nums = {1, 2};
std::vector subsets;
for (int num : nums) {
int n = subsets.size();
for (int i = 0; i < n; i++) {
std::vector<int> newSubset = subsets[i];
newSubset.push_back(num);
subsets.push_back(newSubset);
}
}
medium
A. subsets vector is not initialized with empty subset before loop
B. Loop variable i should start from 1, not 0
C. newSubset.push_back(num) should be outside inner loop
D. nums vector should be empty to generate subsets
Solution
Step 1: Check initialization of subsets
subsets is empty initially, so subsets.size() is 0, inner loop never runs.
Step 2: Identify missing empty subset
We must start subsets with empty subset {{}} to build new subsets correctly.
Final Answer:
subsets vector is not initialized with empty subset before loop -> Option A
Quick Check:
Empty subset needed to start building subsets [OK]
Hint: Always start subsets with empty subset {{}} [OK]
Common Mistakes:
Not initializing subsets with empty subset
Changing loop start index incorrectly
Misplacing push_back calls
5. Given a vector nums = {1, 2, 2}, which code modification ensures the generated subsets contain no duplicate subsets?
hard
A. Generate subsets normally; duplicates are impossible with vector<int>.
B. Use a set to store subsets after generation to remove duplicates.
C. Ignore duplicate elements in nums vector before generating subsets.
D. Sort nums first and skip adding duplicates during subset generation.
Solution
Step 1: Understand duplicates in input
Input has duplicate 2's, so subsets like {2} can appear multiple times.
Step 2: Use sorting and skip duplicates
Sorting nums helps detect duplicates; skip adding subsets with same element consecutively to avoid duplicates.
Final Answer:
Sort nums first and skip adding duplicates during subset generation. -> Option D