Counting Bits
Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i.

30:00

Counting Bits
easy
Topics
Companies

Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i.

Example 1:
Input: 5
Output: [0,1,1,2,1,2]
Constraints:
  • 0n1050 \leq n \leq 10^5

Input
arr =5

Initialize result array with zeros

Result Array (count of 1s for each number)

0

0

0

1

0

2

0

3

0

4

0

5

1 bit

Current

Variables
No variables to display
DepthFunction Call
Stack empty
0/6