2Brute2Best
πŸ”₯0

Counting Bits

EasyΒ·LeetCode #338 Pro OnlySlight Variation
LeetCode
01

Problem Statement

Given n, build an array of n + 1 numbers. The number at index i is how many 1 bits i has in binary, for every i from 0 to n. Do not use built-in bit-count functions.

Follow-up: it is easy to get O(N log N) time. Can you do it in O(N) time, in a single pass?

Input

An integer n.

Output

An array ans of length n + 1, where ans[i] is the number of 1 bits in i.

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

0 has no 1 bits. 1 = 1 has one. 2 = 10 has one.

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

3 = 11 has two 1 bits, 4 = 100 has one, and 5 = 101 has two.

Constraints
  • β€’ 0 ≀ n ≀ 10^5
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œnumber of 1 bits for each i from 0 to n”
β†’ Many answers, one per number. Reuse earlier answers instead of counting from scratch.
β€œdo not use built-in bit counting”
β†’ The shift rule itself has to be found: `i >> 1` plus the last bit.
β€œlinear time, single pass”
β†’ Each number is solved from one smaller number, in increasing order.
03

Approach Complexity Ladder

Brute Force β€” count each number's bitsO(N log N)O(1)

For each i, shift it right and add up its bits

Optimal β€” reuse the answer for i >> 1O(N)O(N)

ans[i] = ans[i >> 1] + (i & 1), filled from 1 up to n

Brute Force Intuition

Count the 1 bits of each i from 0 to n on its own. Each count shifts i right one bit at a time and adds the lowest bit.

Recognition rule for this sub-pattern:

n & (nβˆ’1) removes the lowest set bit; shifts read bits one at a time.
Remember this Β· Bit Manipulation
Trigger
"Every element appears twice except…", power of two, counting set bits, subsets as bitmasks, O(1) space tricks.
Think
What happens to this problem if I look at each bit separately?
Key question
Which bit operation (XOR, AND, OR, shift) cancels or isolates what I need?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Bit Manipulation translate into production architectures and low-level memory mechanics.