Data Structures & AlgorithmsHardCodeforces Div2 C · GATE CS

What is the value of sum of popcount(i) for i from 1 to 20 (number of 1-bits in the binary form of each i, summed)?

  1. A. 40
  2. B. 43
  3. C. 45
  4. D. 42

Correct answer: D. 42

Using the recurrence popcount(i) = popcount(i >> 1) + (i & 1), or summing directly, gives 42 for 1 to 20. Dropping the term for 20 itself gives 40.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic