Skip to content

Files

Latest commit

 

History

History
27 lines (21 loc) · 2.07 KB

File metadata and controls

27 lines (21 loc) · 2.07 KB

Counting Bits easy #javascript #blind75 #bit-manipulation #dynamic-programming

by Pawan Kumar @jsartisan

Take the Challenge

Given an integer n, return an array where output[i] is the number of 1's in the binary representation of i for all numbers from 0 to n.

Rules:

  • Return array of length n+1
  • Each element represents count of 1's in binary form of index
  • Array starts from 0 and goes up to n

Constraints:

  • 0 ≤ n ≤ 1000

Examples:

// Example 1:
console.log(countBits(4));
// Output: [0,1,1,2,1]
// Explanation:
// 0 -> 0     -> 0 ones
// 1 -> 1     -> 1 one
// 2 -> 10    -> 1 one
// 3 -> 11    -> 2 ones
// 4 -> 100   -> 1 one

Back Share your Solutions Check out Solutions