Given a positive integer n, count the total number of set bits in binary representation of all numbers from 1 to n.
Input: n = 3 Output: 4 Input: n = 6 Output: 9 Input: n = 7 Output: 12 Input: n = 8 Output: 13
Source: Amazon Interview Question
Method 1 (Simple)
A simple solution is to run a loop from 1 to n and sum the count of set bits in all numbers from 1 to n.
Total set bit count is 5
Time Complexity: O(nLogn)
Method 2 (Simple and efficient than Method 1)
If we observe bits from rightmost side at distance i than bits get inverted after 2^i position in vertical sequence.
for example n = 5;
0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
Observe the right most bit (i = 0) the bits get flipped after (2^0 = 1)
Observe the 3nd rightmost bit (i = 2) the bits get flipped after (2^2 = 4)
So, We can count bits in vertical fashion such that at i’th right most position bits will be get flipped after 2^i iteration;
Time Complexity: O(k*n)
where k = number of bits to represent number n
k <= 64
Method 3 (Tricky)
If the input number is of the form 2^b -1 e.g., 1, 3, 7, 15.. etc, the number of set bits is b * 2^(b-1). This is because for all the numbers 0 to (2^b)-1, if you complement and flip the list you end up with the same list (half the bits are on, half off).
If the number does not have all set bits, then some position m is the position of leftmost set bit. The number of set bits in that position is n – (1 << m) + 1. The remaining set bits are in two parts:
1) The bits in the (m-1) positions down to the point where the leftmost bit becomes 0, and
2) The 2^(m-1) numbers below that point, which is the closed form above.
An easy way to look at it is to consider the number 6:
0|0 0 0|0 1 0|1 0 0|1 1 -|-- 1|0 0 1|0 1 1|1 0
The leftmost set bit is in position 2 (positions are considered starting from 0). If we mask that off what remains is 2 (the “1 0” in the right part of the last row.) So the number of bits in the 2nd position (the lower left box) is 3 (that is, 2 + 1). The set bits from 0-3 (the upper right box above) is 2*2^(2-1) = 4. The box in the lower right is the remaining bits we haven’t yet counted, and is the number of set bits for all the numbers up to 2 (the value of the last entry in the lower right box) which can be figured recursively.
Total set bit count is 35
Time Complexity: O(Logn). From the first look at the implementation, time complexity looks more. But if we take a closer look, statements inside while loop of getNextLeftmostBit() are executed for all 0 bits in n. And the number of times recursion is executed is less than or equal to set bits in n. In other words, if the control goes inside while loop of getNextLeftmostBit(), then it skips those many bits in recursion.
Thanks to agatsu and IC for suggesting this solution.
Here is another solution suggested by Piyush Kapoor.
A simple solution , using the fact that for the ith least significant bit, answer will be
X = N%(2^i)-(2^(i-1)-1)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Length of the Longest Consecutive 1s in Binary Representation
- Swap all odd and even bits
- Binary representation of a given number
- Find the element that appears once
- Detect if two integers have opposite signs
- Swap bits in a given number
- Add two numbers without using arithmetic operators
- Program to count number of set bits in an (big) array
- Next higher number with same number of set bits
- Rotate bits of a number
- Count number of bits to be flipped to convert A to B
- Count set bits in an integer
- Write an Efficient C Program to Reverse Bits of a Number
- Position of rightmost set bit
- Program to find whether a no is power of two