Count total unset bits in all numbers from 1 to n
The problem we're addressing is to count the total number of unset bits (also known as inactive bits or 0s) in the
binary representation of all numbers from 1 to a given positive integer n
. This problem involves
calculating the sum of unset bits in the binary representation of each number within the range.
Problem Statement and Description
Given a positive integer n
, the task is to count the total number of unset bits (0s) in the binary
representation of all numbers from 1 to n
. In other words, we need to sum up the number of 0s in the
binary representation of each number within the range [1, n].
Example
Consider the number 7. The binary representations of the numbers from 1 to 7 are as follows:
 1: 1 (0 unset bits)
 2: 10 (1 unset bits)
 3: 11 (0 unset bits)
 4: 100 (2 unset bits)
 5: 101 (1 unset bits)
 6: 110 (1 unset bits)
 7: 111 (0 unset bits)
The total number of unset bits in these binary representations is 5. Therefore, the expected output for this example should be "Unset bits from (1 to 7): 5".
Idea to Solve the Problem
The main idea to solve this problem is to utilize the properties of unset bits in the binary representation. We can
use the countSetBit
function (which you provided) to calculate the number of set bits in a single
number. Then, we can calculate the number of unset bits by subtracting the count of set bits from the total number
of bits in the binary representation.
Pseudocode
Here's the pseudocode for the algorithm:
function countSetBit(num):
// Count set bit
function countUnsetBit(n):
result = 0
for i from 1 to n:
total_bits = log(i) / log(2.0) + 1.0
result += total_bits  countSetBit(i)
print "Unset bits from (1 to ", n, "): ", result
Algorithm Explanation
countSetBit(num)
calculates the number of set bits (active bits) in the binary representation of a given number using your provided implementation.countUnsetBit(n)
initializes a variableresult
to store the total count of unset bits. It then iterates through the numbers from 1 to
n
using a loop.  For each number
i
, it calculates the total number of bits in its binary representation by using the formulalog(i) / log(2.0) + 1.0
.  It subtracts the count of set bits in the binary representation of
i
from the total number of bits to get the count of unset bits.  After the loop completes, the
result
variable will contain the total number of unset bits in the binary representations of all numbers from 1 ton
.  Print the results.
Code Solution

1) Count valid unset bits in numbers from 1 to n in java
2) Count valid unset bits in numbers from 1 to n in c++
3) Count valid unset bits in numbers from 1 to n in c
4) Count valid unset bits in numbers from 1 to n in c#
5) Count valid unset bits in numbers from 1 to n in vb.net
6) Count valid unset bits in numbers from 1 to n in php
7) Count valid unset bits in numbers from 1 to n in node js
8) Count valid unset bits in numbers from 1 to n in typescript
9) Count valid unset bits in numbers from 1 to n in python
10) Count valid unset bits in numbers from 1 to n in ruby
11) Count valid unset bits in numbers from 1 to n in scala
12) Count valid unset bits in numbers from 1 to n in swift
13) Count valid unset bits in numbers from 1 to n in kotlin
14) Count valid unset bits in numbers from 1 to n in golang
Time Complexity
The time complexity of the algorithm primarily depends on the iteration through the numbers from 1 to n
and the calculation of unset bits using the countSetBit
function and the logarithmic operation. Since
these operations are performed sequentially, the overall time complexity can be approximated as O(n * log n), where
n
is the input number.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment