Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Counting Bits

Easy
bit-manipulationdynamic-programming

For every i from 0 to n, output the number of 1 bits in i. Aim for O(n) total — better than calling popcount n times.

Input: one line, the integer n. Output: n+1 counts, space-separated.

Example 1
Input:
2
Output:
0 1 1
Example 2
Input:
5
Output:
0 1 1 2 1 2

Constraints

  • 0 ≤ n ≤ 10^5
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.