FastPrepCounting Bits

Counting Bits

GoodScore logoGoodScore● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

For every integer from 0 through n, return the number of set bits in its binary representation.

Function

countBits(n: int) → int[]

Examples

Example 1

n = 0return = [0]

Case 1 exercises the documented deterministic contract.

Example 2

n = 1return = [0,1]

Case 2 exercises the documented deterministic contract.

Example 3

n = 2return = [0,1,1]

Case 3 exercises the documented deterministic contract.

Constraints

  • 0 <= n <= 1000000.

More GoodScore problems

See GoodScore hiring insights
public int[] countBits(int n) {
    // Write your code here.
}
n0
expected[0]
Checking account…