Given a positive integer N, the task is to calculate the sum of all decimals which can be expressed as binary representations of first N natural numbers.
Examples:
Input: N = 3
Output: 22
Explanation:
The Binary Representation of 1 is 01.
The Binary Representation of 2 is 10.
The Binary Representation of 3 is 11.
Therefore, required sum = 01 + 10 + 11 = 22.
Input: N = 5
Output: 223
Naive Approach: The simplest approach to solve the problem is to iterate a loop over the range [1, N] and in each iteration convert the current number to its binary representation and add it to the overall sum. After adding all the numbers, print the sum as the result.
Time Complexity: O(N * log(N))
Auxiliary Space: O(32)
Efficient Approach: The above approach can also be optimized by finding the contribution of numbers not having the same most significant bit (MSB) position as N and then find the contribution by the MSB of the rest of the numbers. Follow the steps to solve the problem:
- Initialize a variable, say ans as 0 to store the sum of all the numbers in the binary representation of first N natural numbers.
- Iterate until the value of N is at least 0, and perform the following steps:
- Store the MSB position of the number N in a variable X and store the value of 2(X – 1) in a variable, say A.
- Initialize a variable, say cur as 0 to store the contribution of numbers not having the same MSB position as N.
- Iterate over the range [1, X], and in each iteration, add A to the variable cur and then multiply A by 10.
- After the above steps, add the value of cur to ans and store the remaining elements in variable rem as (N – 2X + 1).
- Add the contribution by the MSB of the rest of the numbers by adding (rem * 10X) to the ans.
- Update the value of N to (rem – 1) for the next iteration.
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
void sumOfBinaryNumbers( int n)
{
int ans = 0;
int one = 1;
while (1) {
if (n <= 1) {
ans = (ans + n) % MOD;
break ;
}
int x = log2(n);
int cur = 0;
int add = (one << (x - 1));
for ( int i = 1; i <= x; i++) {
cur = (cur + add) % MOD;
add = (add * 10 % MOD);
}
ans = (ans + cur) % MOD;
int rem = n - (one << x) + 1;
int p = pow (10, x);
p = (p * (rem % MOD)) % MOD;
ans = (ans + p) % MOD;
n = rem - 1;
}
cout << ans;
}
int main()
{
int N = 3;
sumOfBinaryNumbers(N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
class GFG
{
static final int MOD = 1000000007 ;
static void sumOfBinaryNumbers( int n)
{
int ans = 0 ;
int one = 1 ;
while ( true ) {
if (n <= 1 ) {
ans = (ans + n) % MOD;
break ;
}
int x = ( int )(Math.log(n) / Math.log( 2 ));
int cur = 0 ;
int add = ( int )(Math.pow( 2 , (x - 1 )));
for ( int i = 1 ; i <= x; i++) {
cur = (cur + add) % MOD;
add = (add * 10 % MOD);
}
ans = (ans + cur) % MOD;
int rem = n - ( int )(Math.pow( 2 , x)) + 1 ;
int p = ( int )Math.pow( 10 , x);
p = (p * (rem % MOD)) % MOD;
ans = (ans + p) % MOD;
n = rem - 1 ;
}
System.out.println(ans);
}
public static void main(String[] args)
{
int N = 3 ;
sumOfBinaryNumbers(N);
}
}
|
Python3
from math import log2, pow
MOD = 1000000007
def sumOfBinaryNumbers(n):
ans = 0
one = 1
while ( 1 ):
if (n < = 1 ):
ans = (ans + n) % MOD
break
x = int (log2(n))
cur = 0
add = (one << (x - 1 ))
for i in range ( 1 , x + 1 , 1 ):
cur = (cur + add) % MOD
add = (add * 10 % MOD)
ans = (ans + cur) % MOD
rem = n - (one << x) + 1
p = pow ( 10 , x)
p = (p * (rem % MOD)) % MOD
ans = (ans + p) % MOD
n = rem - 1
print ( int (ans))
if __name__ = = '__main__' :
N = 3
sumOfBinaryNumbers(N)
|
C#
using System;
class GFG{
const int MOD = 1000000007;
static void sumOfBinaryNumbers( int n)
{
int ans = 0;
int one = 1;
while ( true )
{
if (n <= 1)
{
ans = (ans + n) % MOD;
break ;
}
int x = ( int )Math.Log(n, 2);
int cur = 0;
int add = (one << (x - 1));
for ( int i = 1; i <= x; i++)
{
cur = (cur + add) % MOD;
add = (add * 10 % MOD);
}
ans = (ans + cur) % MOD;
int rem = n - (one << x) + 1;
int p = ( int )Math.Pow(10, x);
p = (p * (rem % MOD)) % MOD;
ans = (ans + p) % MOD;
n = rem - 1;
}
Console.WriteLine(ans);
}
public static void Main()
{
int N = 3;
sumOfBinaryNumbers(N);
}
}
|
Javascript
<script>
let MOD = 1000000007;
function sumOfBinaryNumbers(n)
{
let ans = 0;
let one = 1;
while ( true ) {
if (n <= 1) {
ans = (ans + n) % MOD;
break ;
}
let x = Math.floor(Math.log(n) / Math.log(2));
let cur = 0;
let add = Math.floor(Math.pow(2, (x - 1)));
for (let i = 1; i <= x; i++) {
cur = (cur + add) % MOD;
add = (add * 10 % MOD);
}
ans = (ans + cur) % MOD;
let rem = n - Math.floor(Math.pow(2, x)) + 1;
let p = Math.floor(Math.pow(10, x));
p = (p * (rem % MOD)) % MOD;
ans = (ans + p) % MOD;
n = rem - 1;
}
document.write(ans);
}
let N = 3;
sumOfBinaryNumbers(N);
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)