Find the number obtained after concatenation of binary representation of M and N
Last Updated :
14 Nov, 2022
Given two integers M and N the task is to find the number formed by concatenating the binary equivalents of M and N i.e. M + N.
Examples:
Input: M = 4, N = 5
Output: 37
Binary equivalent of 4 is 100 and for 5 it is 101
after concatenation, the resultant binary number
formed is 100101 whose decimal equivalent is 37.
Input: M = 3, N = 4
Output: 28
Approach: Convert both the numbers M and N into their binary equivalents then concatenate these numbers as M + N and print the decimal equivalent of the resulting binary number formed after concatenation.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void decBinary( int arr[], int n)
{
int k = log2(n);
while (n > 0) {
arr[k--] = n % 2;
n /= 2;
}
}
int binaryDec( int arr[], int n)
{
int ans = 0;
for ( int i = 0; i < n; i++)
ans += arr[i] << (n - i - 1);
return ans;
}
int concat( int m, int n)
{
int k = log2(m) + 1;
int l = log2(n) + 1;
int a[k] = { 0 }, b[l] = { 0 };
int c[k + l] = { 0 };
decBinary(a, m);
decBinary(b, n);
int in = 0;
for ( int i = 0; i < k; i++)
c[in++] = a[i];
for ( int i = 0; i < l; i++)
c[in++] = b[i];
return (binaryDec(c, k + l));
}
int main()
{
int m = 4, n = 5;
cout << concat(m, n);
return 0;
}
|
Java
class GFG
{
static void decBinary( int arr[], int n)
{
int k = ( int )(Math.log(n) /
Math.log( 2 ));
while (n > 0 )
{
arr[k--] = n % 2 ;
n /= 2 ;
}
}
static int binaryDec( int arr[], int n)
{
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
ans += arr[i] << (n - i - 1 );
return ans;
}
static int concat( int m, int n)
{
int k = ( int )(Math.log(m) /
Math.log( 2 )) + 1 ;
int l = ( int )(Math.log(n) /
Math.log( 2 )) + 1 ;
int a[] = new int [k];
int b[] = new int [l];
int c[] = new int [k + l];
decBinary(a, m);
decBinary(b, n);
int in = 0 ;
for ( int i = 0 ; i < k; i++)
c[in++] = a[i];
for ( int i = 0 ; i < l; i++)
c[in++] = b[i];
return (binaryDec(c, k + l));
}
public static void main (String[] args)
{
int m = 4 , n = 5 ;
System.out.println(concat(m, n));
}
}
|
Python3
import math
def decBinary(arr, n):
k = int (math.log2(n))
while (n > 0 ):
arr[k] = n % 2
k = k - 1
n = n / / 2
def binaryDec(arr, n):
ans = 0
for i in range ( 0 , n):
ans = ans + (arr[i] << (n - i - 1 ))
return ans
def concat(m, n):
k = int (math.log2(m)) + 1
l = int (math.log2(n)) + 1
a = [ 0 for i in range ( 0 , k)]
b = [ 0 for i in range ( 0 , l)]
c = [ 0 for i in range ( 0 , k + l)]
decBinary(a, m);
decBinary(b, n);
iin = 0
for i in range ( 0 , k):
c[iin] = a[i]
iin = iin + 1
for i in range ( 0 , l):
c[iin] = b[i]
iin = iin + 1
return (binaryDec(c, k + l))
m = 4
n = 5
print (concat(m, n))
|
C#
using System;
class GFG
{
static void decBinary( int []arr, int n)
{
int k = ( int )(Math.Log(n) /
Math.Log(2));
while (n > 0)
{
arr[k--] = n % 2;
n /= 2;
}
}
static int binaryDec( int []arr, int n)
{
int ans = 0;
for ( int i = 0; i < n; i++)
ans += arr[i] << (n - i - 1);
return ans;
}
static int concat( int m, int n)
{
int k = ( int )(Math.Log(m) /
Math.Log(2)) + 1;
int l = ( int )(Math.Log(n) /
Math.Log(2)) + 1;
int []a = new int [k];
int []b = new int [l];
int []c = new int [k + l];
decBinary(a, m);
decBinary(b, n);
int iN = 0;
for ( int i = 0; i < k; i++)
c[iN++] = a[i];
for ( int i = 0; i < l; i++)
c[iN++] = b[i];
return (binaryDec(c, k + l));
}
public static void Main(String[] args)
{
int m = 4, n = 5;
Console.WriteLine(concat(m, n));
}
}
|
Javascript
<script>
function decBinary(arr, n)
{
let k = parseInt(Math.log(n) / Math.log(2), 10);
while (n > 0)
{
arr[k--] = n % 2;
n = parseInt(n / 2, 10);
}
}
function binaryDec(arr, n)
{
let ans = 0;
for (let i = 0; i < n; i++)
ans += arr[i] << (n - i - 1);
return ans;
}
function concat(m, n)
{
let k = parseInt(Math.log(m) / Math.log(2), 10) + 1;
let l = parseInt(Math.log(n) / Math.log(2), 10) + 1;
let a = new Array(k);
let b = new Array(l);
let c = new Array(k + l);
decBinary(a, m);
decBinary(b, n);
let iN = 0;
for (let i = 0; i < k; i++)
c[iN++] = a[i];
for (let i = 0; i < l; i++)
c[iN++] = b[i];
return (binaryDec(c, k + l));
}
let m = 4, n = 5;
document.write(concat(m, n));
</script>
|
Time Complexity: O(log(m) + log(n)), We can improve the time complexity for the above problem to O(log(n)) by using binary shift operators.
Auxiliary Space: O(1)
Better Approach: We will binary shift the number M to left by the number of bits in n.
Example:
M=4, N=5
bin(N) = 101 which has binary representation three.
We will left binary shift M by 3 and then add N.
M<<3 + N = 37.
As binary shift operators take constant time so second step is done in constant time and overall time complexity is O(log(N)).
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getBinaryLength( int n)
{
int length = 0;
while (n > 0) {
length += 1;
n /= 2;
}
return length;
}
int concat( int m, int n)
{
int length = getBinaryLength(n);
return (m << length) + n;
}
int main()
{
int m = 4, n = 5;
cout << concat(m, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static int getBinaryLength( int n)
{
int length = 0 ;
while (n > 0 )
{
length += 1 ;
n /= 2 ;
}
return length;
}
public static int concat( int m, int n)
{
int length = getBinaryLength(n);
return (m << length) + n;
}
public static void main(String[] args) throws Exception
{
int m = 4 , n = 5 ;
System.out.println(concat(m, n));
}
}
|
Python3
def getBinaryLength(n):
length = 0
while (n > 0 ):
length + = 1
n / / = 2
return length
def concat(m, n):
length = getBinaryLength(n)
return (m << length) + n
m, n = 4 , 5
print (concat(m, n))
|
C#
using System;
class GFG{
static int getBinaryLength( int n)
{
int length = 0;
while (n > 0)
{
length += 1;
n /= 2;
}
return length;
}
static int concat( int m, int n)
{
int length = getBinaryLength(n);
return (m << length) + n;
}
static void Main()
{
int m = 4, n = 5;
Console.WriteLine(concat(m, n));
}
}
|
Javascript
<script>
function getBinaryLength(n)
{
let length = 0;
while (n > 0)
{
length += 1;
n = parseInt(n / 2, 10);
}
return length;
}
function concat(m, n)
{
let length = getBinaryLength(n);
return (m << length) + n;
}
let m = 4, n = 5;
document.write(concat(m, n));
</script>
|
Time Complexity: O(log(n)).
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...