Data Structures & AlgorithmsHardCodeforces Div2 B · bit counting
Problem: What is the sum of the number of 1-bits in the binary representations of 1, 2, ..., 20? Run the code.
Problem: What is the sum of the number of 1-bits in the binary representations of 1, 2, ..., 20? Run the code.
print(sum(bin(i).count('1') for i in range(1, 21)))What is printed?- A. 40
- B. 42
- C. 38
- D. 44
Correct answer: B. 42
Summing popcount over 1..20 gives 42. Using a 2-to-the-bits shortcut that counts only up to 16 misses the bits of 17 to 20.
More Coding Interview Problems questions
- Problem: Which statement about shortest paths in a weighted directed acyclic graph (DAG) is correct?
- Problem: Which statement about Floyd-Warshall on a graph with V = 400 vertices and no negative cycles is correct?
- Problem: Minimum number of taps needed to water a garden of length 5 is found from tap i covering [i - ri, i + ri]. Here n = 5 and r = [3…
- Problem: A stone game: piles [5, 3, 4, 5] lie in a row, and players alternately take a pile from either end. The first player wants to…
- Problem: Prim's algorithm starts at vertex 0 on the undirected weighted graph with edges 0-1 (6), 0-3 (2), 1-2 (3), 1-3 (8), 1-4 (5), 2-4…
- Problem: A directed graph on vertices 0–5 has arcs 0→1, 1→2, 2→0, 2→3, 3→4, 4→3, 4→5. Its strongly connected components are computed. How…