v9.1.1
Menu

Counting Bits

easyFREEBit ManipulationBlind 75

Given an integer n, return an array of length n+1 where ans[i] is the number of 1s in the binary representation of i.

Example

Input
5
Output
0 1 1 2 1 2
Reasoning
  • The input 5 is the upper end of a range: you report on every integer from 00 up to 55, so the result holds 5+1=65 + 1 = 6 counts, one per integer, in order.
  • Each count is the number of 11 digits when that integer is written in base 22 (its popcount):
    • 0=020 = 0_2 has 00 ones; 1=121 = 1_2 has 11
    • 2=1022 = 10_2 has 11; 3=1123 = 11_2 has 22
    • 4=10024 = 100_2 has 11; 5=10125 = 101_2 has 22
  • Reading those counts left to right gives the output 0 1 1 2 1 2: position ii holds the count for the integer ii.
  • Notice the counts are not monotonic: 44 has fewer ones than 33, because every power of two (1,2,4,…1, 2, 4, \dots) is a single 11 followed by zeros.
  • Edge case: for n=0n = 0 the result has one entry, 0. For n=2n = 2 it is 0 1 1.

Constraints

  • 0 <= n <= 10^5

More from Blind 75

solution.py

Test results

Sign in to run your code against the test cases.