Skip to content

Practice Counting Bits

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#338
LeetCode Problem

Counting Bits

Problem Statement

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.

Source: Counting Bits on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 2
Output
[0,1,1]

# Example 2

Input
n = 5
Output
[0,1,1,2,1,2]

Constraints

0 <= n <= 10^5

Related LeetCode Problems

Practice more problems similar to Counting Bits.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.