Data Structures & AlgorithmsHardCodeforces Div2 C · bitmask counting

Problem: How many subsets (including the empty subset) of [1, 2, 3] have XOR equal to 0? Run the code.

Problem: How many subsets (including the empty subset) of [1, 2, 3] have XOR equal to 0? Run the code.
a = [1, 2, 3]
print(sum(1 for m in range(1 << len(a)) if (lambda s: s)(__import__('functools').reduce(lambda x, i: x ^ a[i], [i for i in range(len(a)) if m >> i & 1], 0)) == 0))
What is printed?
  1. A. 1
  2. B. 2
  3. C. 3
  4. D. 4

Correct answer: B. 2

Only the empty subset and {1, 2, 3} have XOR 0, since 1 XOR 2 XOR 3 = 0. Counting only non-empty subsets gives the trap 1.

More Coding Interview Problems questions

Exams that ask this topic