Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many subsets of {1, 2, 3, 4, 5, 6} have sum exactly 10?

Problem: How many subsets of {1, 2, 3, 4, 5, 6} have sum exactly 10?
print(sum(1 for m in range(1 << 6) if sum(i + 1 for i in range(6) if m >> i & 1) == 10))
What is printed?
  1. A. 6
  2. B. 4
  3. C. 5
  4. D. 7

Correct answer: C. 5

Each bitmask selects one subset; counting those whose element sum is 10 gives the answer.

More Coding Interview Problems questions

Exams that ask this topic