VISUALGO//PROBLEMS
counting bits · bit manipulationeasy
i
·
bits
·
i in binary
·
·
·
·
i >> 1
·
·
·
·
ans
·
·
·
·
·
·
ELEMENTS
n
Python
def countBits(n: int) -> list[int]:
ans = [0] * (n + 1)
for i in range(1, n + 1):
ans[i] = ans[i >> 1] + (i & 1)
return ans