Sum of bit differences for numbers from 0 to N
Last Updated :
02 Nov, 2023
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.
Examples:
Input: N = 5
Output: 8
Explanation:
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
Output: 19
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).
-
For Examples:
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:
C++
#include <bits/stdc++.h>
using namespace std;
int binpow( int a, int b)
{
int res = 1;
while (b) {
if (b & 1)
res = res * a;
a = a * a;
b /= 2;
}
return res;
}
int find( int x)
{
if (x == 0)
return 0;
int p = log2(x);
return binpow(2, p + 1) - 1;
}
string getBinary( int n)
{
string ans = "" ;
while (n) {
int dig = n % 2;
ans += to_string(dig);
n /= 2;
}
return ans;
}
int totalCountDifference( int n)
{
string ans = getBinary(n);
int req = 0;
for ( int i = 0; i < ans.size(); i++) {
if (ans[i] == '1' ) {
req += find(binpow(2, i));
}
}
return req;
}
int main()
{
int N = 5;
cout << totalCountDifference(N);
return 0;
}
|
Java
import java.io.*;
import java.lang.Math;
class GFG{
static int binpow( int a, int b)
{
int res = 1 ;
while (b > 0 )
{
if (b % 2 == 1 )
res = res * a;
a = a * a;
b /= 2 ;
}
return res;
}
static int find( int x)
{
if (x == 0 )
return 0 ;
int p = ( int )(Math.log(x) / Math.log( 2 ));
return binpow( 2 , p + 1 ) - 1 ;
}
static String getBinary( int n)
{
String ans = "" ;
while (n > 0 )
{
int dig = n % 2 ;
ans += dig;
n /= 2 ;
}
return ans;
}
static int totalCountDifference( int n)
{
String ans = getBinary(n);
int req = 0 ;
for ( int i = 0 ; i < ans.length(); i++)
{
if (ans.charAt(i) == '1' )
{
req += find(binpow( 2 , i));
}
}
return req;
}
public static void main (String[] args)
{
int n = 5 ;
System.out.print(totalCountDifference(n));
}
}
|
Python3
from math import log
def binpow(a, b):
res = 1
while (b > 0 ):
if (b % 2 = = 1 ):
res = res * a
a = a * a
b / / = 2
return res
def find(x):
if (x = = 0 ):
return 0
p = log(x) / log( 2 )
return binpow( 2 , p + 1 ) - 1
def getBinary(n):
ans = ""
while (n > 0 ):
dig = n % 2
ans + = str (dig)
n / / = 2
return ans
def totalCountDifference(n):
ans = getBinary(n)
req = 0
for i in range ( len (ans)):
if (ans[i] = = '1' ):
req + = find(binpow( 2 , i))
return req
N = 5
print (totalCountDifference(N))
|
C#
using System;
class GFG{
static int binpow( int a, int b)
{
int res = 1;
while (b > 0)
{
if (b % 2 == 1)
res = res * a;
a = a * a;
b /= 2;
}
return res;
}
static int find( int x)
{
if (x == 0)
return 0;
int p = ( int )(Math.Log(x) / Math.Log(2));
return binpow(2, p + 1) - 1;
}
static String getBinary( int n)
{
String ans = "" ;
while (n > 0)
{
int dig = n % 2;
ans += dig;
n /= 2;
}
return ans;
}
static int totalCountDifference( int n)
{
string ans = getBinary(n);
int req = 0;
for ( int i = 0; i < ans.Length; i++)
{
if (ans[i] == '1' )
{
req += find(binpow(2, i));
}
}
return req;
}
public static void Main()
{
int n = 5;
Console.Write(totalCountDifference(n));
}
}
|
Javascript
<script>
function binpow(a, b) {
let res = 1;
while (b) {
if (b & 1)
res = res * a;
a = a * a;
b = Math.floor(b / 2);
}
return res;
}
function find(x) {
if (x == 0)
return 0;
let p = Math.log2(x);
return binpow(2, p + 1) - 1;
}
function getBinary(n) {
let ans = "" ;
while (n) {
let dig = n % 2;
ans += String(dig);
n = Math.floor(n / 2);
}
return ans;
}
function totalCountDifference(n)
{
let ans = getBinary(n);
let req = 0;
for (let i = 0; i < ans.length; i++) {
if (ans[i] == '1' ) {
req += find(binpow(2, i));
}
}
return req;
}
let N = 5;
document.write(totalCountDifference(N));
</script>
|
Time Complexity: O((log N)2)
Auxiliary Space: (1)
Share your thoughts in the comments
Please Login to comment...