Problem statement

https://binarysearch.com/problems/Number-of-Bits/

Solution

Equal to Leetcod 0338. Counting Bits

Complexity

It is O(log n) for time and space. Or it can be better, see other solutions.

Code

class Solution:
    def solve(self, n):
        return bin(n).count("1")