Quant Memo
Coding/●●●●●

Total set bits across 1..n

Given n, compute the total number of 1-bits across all integers from 1 to n.

n = 5
-> 7      # popcounts are [0, 1, 1, 2, 1, 2], and 1+1+2+1+2 = 7

Return the running total. Counting each number independently is O(nlogn)O(n \log n), aim for O(n)O(n).

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions