Bird
Raised Fist0

What is the time complexity of generating all subsets of an array of size n using bitmask enumeration, and why?

medium🪤 Complexity Trap Q5 of Q15
Subsets & Combinations - Subsets Using Bitmask
What is the time complexity of generating all subsets of an array of size n using bitmask enumeration, and why?
AO(n^2) because each subset requires scanning all elements
BO(2^n) because subsets are generated directly without scanning elements
CO(n * 2^n) because there are 2^n subsets and each subset takes O(n) to build
DO(n) because bitmasking is constant time per subset
Step-by-Step Solution
Solution:
  1. Step 1: Count subsets

    There are 2^n subsets for n elements.
  2. Step 2: Time per subset

    Each subset requires checking up to n bits to build the subset list.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Time = O(n * 2^n) due to subset count and element checks [OK]
Quick Trick: 2^n subsets x n elements each [OK]
Common Mistakes:
MISTAKES
  • Confusing O(2^n) with O(n * 2^n)
  • Ignoring element scanning cost
Trap Explanation:
PITFALL
  • Candidates often forget that building each subset requires scanning elements, leading to underestimating complexity.
Interviewer Note:
CONTEXT
  • Tests understanding of exponential time complexity in subset generation.
Master "Subsets Using Bitmask" in Subsets & Combinations

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Subsets & Combinations Quizzes