- Create a loop which runsfor
n+1times. - Inside the loop i find the binary representation for the index value.
- I then move to find the amount of
1in the binary representation and add it to the ans array
-
Time complexity:O(NLogN)
-
Space complexity:O(N)
function countBits(n: number): number[] {
let ans:number[] = []
for (let i = 0; i < n+1; i++) {
let bin =i.toString(2)
let ones = bin.split("").filter((num) => num === "1").length
ans.push(Number(ones))
}
return ans
};