Given a number N, the task is to calculate the total number of corresponding different bit in the binary representation for every consecutive number from 0 to N.
Input: N = 5
Binary Representation of numbers are:
0 -> 000,
1 -> 001,
2 -> 010,
3 -> 011,
4 -> 100,
5 -> 101
Between 1 and 0 -> 1 bit is different
Between 2 and 1 -> 2 bits are different
Between 3 and 2 -> 1 bit is different
Between 4 and 3 -> 3 bits are different
Between 5 and 4 -> 1 bit is different
Total = 1 + 2 + 1 + 3 + 1 = 8
Input: N = 11
Naive Approach: The idea is to iterate from 0 to N and for each pair of a consecutive elements, find the number of different bits for each pair of elements using the approach discussed in this article.
Time Complexity: O(N*log N)
Auxiliary Space: (1)
Efficient Approach: For the efficient approach we have to observe the following:
number: 1 2 3 4 5 6 7 8 bit_diff: 1 2 1 3 1 2 1 4 sum_diff: 1 3 4 7 8 10 11 15
From the above calculations we can observe the following:
- If N is a perfect power of 2, then the total sum of corresponding different bits from 0 to N is given by:
sum_diff = (2x+1-1)
where x = log2(N)
- If N is not a perfect power of 2, then it can be represented as sum of perfect power of 2 as:
N = 2a + 2b + … + 2x
Therefore the total sum of corresponding different bits from 0 to N can be calculated as:
sum_diff = (2a+1-1) + (2b+1-1) + … + (2x+1-1).
If N = 8, then
The binary representation of 8 is “1000”
Hence 11 = 23
total count = (23+1 – 1)
total count = 8 – 1 = 7.
If N = 11, then
The binary representation of 11 is “1011”
Hence 11 = 23 + 21 + 20
=> total count = (23+1 – 1) + (21+1 – 1) + (20+1 – 1)
=> total count = 15 + 3 + 1 = 19.
Below is the implementation of the above approach:
Time Complexity: O((log N)2)
Auxiliary Space: (1)
- Sum of bit differences for numbers from 0 to N | Set 2
- Sum of bit differences among all pairs
- Sum of consecutive bit differences of first N non-negative integers
- Sequence with sum K and minimum sum of absolute differences between consecutive elements
- Inserting m into n such that m starts at bit j and ends at bit i.
- Inserting M into N such that m starts at bit j and ends at bit i | Set-2
- Count of 1-bit and 2-bit characters in the given binary string
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Sum of all the prime numbers with the maximum position of set bit ≤ D
- Maximize the sum of differences of consecutive elements after removing exactly K elements
- Array formed using sum of absolute differences of that element with all other elements
- Sum of all differences between Maximum and Minimum of increasing Subarrays
- Check if two numbers are bit rotations of each other or not
- 1 to n bit numbers with no consecutive 1s in binary representation.
- 1 to n bit numbers with no consecutive 1s in binary representation
- Position of rightmost common bit in two numbers
- Check whether the two numbers differ at one bit position only
- Find position of left most dis-similar bit for two numbers
- Count of numbers having only 1 set bit in the range [0, n]
- Find average of two numbers using bit operation
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.