Data Structures & AlgorithmsHardICPC regional · bitmask round
Problem: How many subsets of {1, 2, 3, 4, 5, 6, 7, 8} have a sum of exactly 10?
Problem: How many subsets of {1, 2, 3, 4, 5, 6, 7, 8} have a sum of exactly 10?
cnt = 0
for m in range(1 << 8):
if sum(i + 1 for i in range(8) if m >> i & 1) == 10:
cnt += 1
print(cnt)What is printed?- A. 6
- B. 10
- C. 12
- D. 8
Correct answer: D. 8
The subsets are {2,8}, {3,7}, {4,6}, {1,2,7}, {1,3,6}, {1,4,5}, {2,3,5} and {1,2,3,4}. The empty set and sets with any element above 8 are not counted.
More Coding Interview Problems questions
- Problem: After running Floyd-Warshall on a directed graph with possibly negative edges, which check correctly detects that vertex i lies…
- Problem: A road graph has V = 10^5 vertices and E = 3 10^5 edges, all weights non-negative, and you need shortest paths from one source…
- Problem: For the shortest subarray with sum at least S in an array of positive integers, a two-pointer sweep is used: extend the right…
- Problem: Weighted interval scheduling (each job has a start, finish and profit; choose a set of non-overlapping jobs to maximise profit)…
- Problem: What does the following print? It returns the next greater element of each position in a circular array (search continues from…
- Problem: How many cells of a 3 x 3 grid are at exactly 2 steps (4-directional moves, no obstacles) from the top-left cell? Run the BFS…