Dashboard
#72. Counting BitsEasyBit Manipulation

Given an integer n, count the number of 1's in the binary representation of every number in the range [0, n].

Return an array output where output[i] is the number of 1's in the binary representation of i.

Example 1:

Input: n = 4

Output: [0,1,1,2,1]

Explanation: 0 --> 0 1 --> 1 2 --> 10 3 --> 11 4 --> 100

Constraints:

  • 0 <= n <= 1000
Examples
Example 1:
Input: n = 4
Output: [0,1,1,2,1]
Constraints
  • `0 <= n <= 1000`
Prerequisites
Target: O(N) time • O(1) spaceDynamic Programming / Bit Manipulation
Socratic AI Tutor
Gemini 3.8 Flash
Loading editor...

Click "Run" or press Ctrl+Enter to execute test cases.