Bird
Raised Fist0

What is the time complexity of generating all subsets of an array of size n using the bit manipulation approach shown below?

medium🪤 Complexity Trap Q13 of Q15
Subsets & Combinations - Subsets
What is the time complexity of generating all subsets of an array of size n using the bit manipulation approach shown below?
AO(2^n * n) because for each of the 2^n masks, we check n bits to build the subset
BO(2^n) because there are 2^n subsets and each subset is generated in constant time
CO(n^2) because of nested loops over n elements
DO(n * 2^n) because each of the 2^n subsets requires iterating over n elements
Step-by-Step Solution
  1. Step 1: Identify outer loop complexity

    The outer loop runs 2^n times, once per subset mask.
  2. Step 2: Identify inner loop complexity

    For each mask, the inner loop iterates n times to check each bit.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Total time is 2^n * n due to mask and bit checks [OK]
Quick Trick: Each subset requires checking n bits -> O(n*2^n) [OK]
Common Mistakes:
MISTAKES
  • Assuming subset generation is O(2^n) ignoring bit checks
  • Confusing n^2 with 2^n * n
Trap Explanation:
PITFALL
  • Option B looks plausible but ignores the cost of iterating over bits per subset.
Interviewer Note:
CONTEXT
  • Checks if candidate understands bitmask iteration cost, not just subset count.
Master "Subsets" 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