ProgrammingModerateGATE CS & IT · Infosys

What is printed?

What is printed?
dp = [0] * 16
for i in range(1, 16):
    dp[i] = dp[i >> 1] + (i & 1)
print(dp[13], sum(dp))
  1. A. 3 28
  2. B. 3 32
  3. C. 4 32
  4. D. 3 30

Correct answer: B. 3 32

dp[i] = dp[i >> 1] + (i & 1) counts the set bits of i. 13 is 1101 with three ones, and the popcounts of 0 to 15 sum to 32.

More Python Programming questions

Exams that ask this topic