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?- A. 1
- B. 2
- C. 3
- 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
- Problem: What does the following print? python print(bin(10).count('1'), (10).bitlength())
- Problem: How many pairs (i < j) of [2, 3, 4, 5, 6] have gcd(a[i], a[j]) = 1? Run the code. python from math import gcd a = [2, 3, 4, 5, 6]…
- What does this Python program print? python a = [2, -2, 3, -3, 1, 1] p = {0: 1}; s = 0; ans = 0 for x in a: s += x ans += p.get(s, 0) p[s]…
- What does this program print? python from collections import Counter a = [3, 1, 2, 3, 1, 2, 3] c = Counter(a) pairs = sum(v (v - 1) // 2…
- What does this program print? python a = [6, 3, 8, 1] mx = [0] len(a) mx[-1] = a[-1] for i in range(len(a) - 2, -1, -1): mx[i] = max(a[i]…
- Array a = [7, -3, 2, 5, -8, 4, 1, -1]. How many contiguous subarrays have a sum divisible by 3? (Zero is divisible by 3.)