29. Count Set Bits
β GFG solution to the Count Set Bits problem: find total count of set bits from 1 to n using mathematical pattern recognition and bit manipulation. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given a number n. Find the total count of set bits for all numbers from 1 to n (both inclusive).
A set bit refers to a bit in the binary representation of a number that is 1. The task is to efficiently count all such bits across the entire range.
π Examples
Example 1
Input: n = 4
Output: 5
Explanation: For numbers from 1 to 4:
- 1: 0 0 1 => 1 set bit
- 2: 0 1 0 => 1 set bit
- 3: 0 1 1 => 2 set bits
- 4: 1 0 0 => 1 set bit
Therefore, the total set bits are 1 + 1 + 2 + 1 = 5.Example 2
π Constraints
$1 \le n \le 10^8$
β
My Approach
The optimal approach uses Mathematical Pattern Recognition with Bit Manipulation to efficiently count set bits:
Recursive Pattern Recognition
Mathematical Insight:
For any number n, we can find the highest power of 2 less than or equal to n (let's call it 2^x).
Count set bits in three parts:
Full pattern from 0 to (2^x - 1)
Contribution of the most significant bit from 2^x to n
Remaining set bits after removing the highest bit
Pattern Formula:
For numbers from 0 to (2^x - 1): Total set bits = x * 2^(x-1)
This is because each bit position i (0 to x-1) is set exactly 2^(x-1) times
MSB contribution from 2^x to n: (n - 2^x + 1) times the MSB is set
Recursive Strategy:
Find highest bit position x using log2(n)
Calculate full pattern bits: x * 2^(x-1)
Calculate MSB contribution: (n - 2^x + 1)
Recursively solve for remaining: countSetBits(n - 2^x)
Base Case:
If n == 0, return 0
This ensures termination of recursion
Optimization:
Using bit operations (1 << x instead of pow(2, x)) for faster computation
Logarithmic recursion depth ensures efficient performance
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(log n), as we recursively process the number by removing the highest bit in each step, leading to logarithmic depth recursion.
Expected Auxiliary Space Complexity: O(log n), due to the recursion stack space. Each recursive call processes a smaller subproblem, resulting in log n depth.
π§ Code (C)
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated