Given a binary integer N, the task is to convert it into base 6.
Note: The number of bits in N is up to 100.
Examples:
Input: N = “100111”
Output: 103
Explanation: The given integer (100111)2 is equivalent to (103)6.
Input: N = “1111111”
Output: 331
Approach: The given problem can be solved by first converting the given integer to decimal, thereafter converting the number from decimal to the base 6 using an approach discussed here. Please note that since the value of the N can reach up to 2100, the 128-bit integer can be used to store the decimal number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void convertBase(string N)
{
__int128 decimal = 0;
for ( int i = 0; i < N.length(); i++) {
decimal = decimal * 2 + (N[i] - '0' );
}
vector< int > ans;
while (decimal > 0) {
ans.push_back(decimal % 6);
decimal = decimal / 6;
}
for ( int i = ans.size() - 1; i >= 0; i--) {
cout << ans[i];
}
}
int main()
{
string N = "100111" ;
convertBase(N);
return 0;
}
|
C
#include <stdio.h>
#include <stdint.h>
#include <string.h>
void convertBase( char * N)
{
__int128 decimal = 0;
int len = strlen (N);
for ( int i = 0; i < len; i++) {
decimal = decimal * 2 + (N[i] - '0' );
}
int ans[len];
int pos = 0;
while (decimal > 0) {
ans[pos++] = (decimal % 6);
decimal = decimal / 6;
}
for ( int i = pos - 1; i >= 0; i--) {
printf ( "%d" , ans[i]);
}
}
int main()
{
char * N = "100111" ;
convertBase(N);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void convertBase(String N)
{
int decimal = 0 ;
for ( int i = 0 ; i < N.length(); i++) {
decimal = decimal * 2 + (N.charAt(i) - '0' );
}
ArrayList<Integer> ans = new ArrayList<Integer>();
while (decimal > 0 ) {
ans.add(decimal % 6 );
decimal = decimal / 6 ;
}
for ( int i = ans.size() - 1 ; i >= 0 ; i--) {
System.out.print(ans.get(i));
}
}
public static void main(String[] args)
{
String N = "100111" ;
convertBase(N);
}
}
|
Python3
def convertBase(N):
decimal = 0
for i in range ( len (N)):
decimal = decimal * 2 + ( ord (N[i]) - ord ( '0' ))
ans = []
while (decimal > 0 ):
ans.append(decimal % 6 )
decimal = decimal / / 6
for i in range ( len (ans) - 1 , - 1 , - 1 ):
print (ans[i], end = "")
N = "100111"
convertBase(N)
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static void convertBase( string N)
{
int decimall = 0;
for ( int i = 0; i < N.Length; i++) {
decimall = decimall * 2 + (N[i] - '0' );
}
List< int > ans = new List< int >();
while (decimall > 0) {
ans.Add(decimall % 6);
decimall = decimall / 6;
}
for ( int i = ans.Count - 1; i >= 0; i--) {
Console.Write(ans[i]);
}
}
public static void Main()
{
string N = "100111" ;
convertBase(N);
}
}
|
Javascript
<script>
function convertBase(N)
{
let decimal = 0;
for (let i = 0; i < N.length; i++)
{
decimal = decimal * 2 +
(N[i].charCodeAt(0) -
'0' .charCodeAt(0));
}
let ans = [];
while (decimal > 0) {
ans.push(decimal % 6);
decimal = Math.floor(decimal / 6);
}
for (let i = ans.length - 1; i >= 0; i--) {
document.write(ans[i]);
}
}
let N = "100111" ;
convertBase(N);
</script>
|
Time Complexity: O(len(N))
Auxiliary Space: O(1)
Another Approach: The given problem can also be solved by maintaining the integer in base 6 in place of the decimal conversion while converting the base of the binary integer to decimal. It is known that the binary number can be converted to a decimal using the following steps:
N = “1001”
N can be converted to (N)10 with the equation: (((1*2 + 0) *2 + 0) *2) + 1).
Hence, two types of steps are required, multiplying the integer by 2 which is equivalent to adding the integer in itself, and adding 0 or 1 to the integer as (0, 1)2 is equivalent to (0, 1)6. Hence, maintain a string representing a base 6 integer, and in each step, add the integer to itself and add 0 or 1 accordingly in each step. If can be done using the approach discussed here.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string sumBaseB(string a, string b, int base)
{
int len_a, len_b;
len_a = a.size();
len_b = b.size();
string sum, s;
s = "" ;
sum = "" ;
int diff;
diff = abs (len_a - len_b);
for ( int i = 1; i <= diff; i++)
s += "0" ;
if (len_a < len_b)
a = s + a;
else
b = s + b;
int curr, carry = 0;
for ( int i = max(len_a, len_b) - 1; i > -1; i--) {
curr = carry + (a[i] - '0' ) + (b[i] - '0' );
carry = curr / base;
curr = curr % base;
sum = ( char )(curr + '0' ) + sum;
}
if (carry > 0)
sum = ( char )(carry + '0' ) + sum;
return sum;
}
string convertBase(string N)
{
string ans = "0" ;
for ( int i = 0; i < N.length(); i++) {
ans = sumBaseB(ans, ans, 6);
ans = sumBaseB(ans, (N[i] == '0' )
? "0"
: "1" ,
6);
}
return ans;
}
int main()
{
string N = "100111" ;
cout << convertBase(N);
return 0;
}
|
Java
class GFG{
static String sumBaseB(String a, String b, int base)
{
int len_a, len_b;
len_a = a.length();
len_b = b.length();
String sum, s;
s = "" ;
sum = "" ;
int diff;
diff = Math.abs(len_a - len_b);
for ( int i = 1 ; i <= diff; i++)
s += "0" ;
if (len_a < len_b)
a = s + a;
else
b = s + b;
int curr, carry = 0 ;
for ( int i = Math.max(len_a, len_b) - 1 ; i > - 1 ; i--) {
curr = carry + (a.charAt(i) - '0' ) + (b.charAt(i) - '0' );
carry = curr / base;
curr = curr % base;
sum = ( char )(curr + '0' ) + sum;
}
if (carry > 0 )
sum = ( char )(carry + '0' ) + sum;
return sum;
}
static String convertBase(String N)
{
String ans = "0" ;
for ( int i = 0 ; i < N.length(); i++) {
ans = sumBaseB(ans, ans, 6 );
ans = sumBaseB(ans, (N.charAt(i) == '0' )
? "0"
: "1" ,
6 );
}
return ans;
}
public static void main(String[] args)
{
String N = "100111" ;
System.out.print(convertBase(N));
}
}
|
Python3
def sumBaseB(a, b, base):
len_a = len (a);
len_b = len (b);
s = ""
sums = ""
diff = abs (len_a - len_b)
for i in range ( 1 , diff + 1 ):
s + = "0"
if (len_a < len_b):
a = s + a
else :
b = s + b
curr, carry = 0 , 0
i = max (len_a, len_b) - 1
while (i > - 1 ):
curr = carry + int (a[i]) + int (b[i])
carry = int (curr / base)
curr = curr % base
sums = str (curr) + sums
i - = 1
if carry > 0 :
sums = str (carry) + sums
return sums
def convertBase(N):
ans = ""
for i in range ( 0 , len (N)):
ans = sumBaseB(ans, ans, 6 )
ans = sumBaseB(ans, [ "1" , "0" ][N[i] = = "0" ], 6 )
return ans
N = "100111"
print (convertBase(N))
|
C#
using System;
class GFG{
static string sumBaseB( string a, string b, int base1)
{
int len_a, len_b;
len_a = a.Length;
len_b = b.Length;
string sum, s;
s = "" ;
sum = "" ;
int diff;
diff = Math.Abs(len_a - len_b);
for ( int i = 1; i <= diff; i++)
s += "0" ;
if (len_a < len_b)
a = s + a;
else
b = s + b;
int curr, carry = 0;
for ( int i = Math.Max(len_a, len_b) - 1; i > -1; i--) {
curr = carry + (a[i] - '0' ) + (b[i] - '0' );
carry = curr / base1;
curr = curr % base1;
sum = ( char )(curr + '0' ) + sum;
}
if (carry > 0)
sum = ( char )(carry + '0' ) + sum;
return sum;
}
static string convertBase( string N)
{
string ans = "0" ;
for ( int i = 0; i < N.Length; i++) {
ans = sumBaseB(ans, ans, 6);
ans = sumBaseB(ans, (N[i] == '0' )
? "0"
: "1" ,
6);
}
return ans;
}
public static void Main( string [] args)
{
string N = "100111" ;
Console.WriteLine(convertBase(N));
}
}
|
Javascript
function sumBaseB(a, b, base)
{
var len_a = a.length;
var len_b = b.length;
var s = "" ;
var sums = "" ;
var diff = Math.abs(len_a - len_b);
for ( var i = 1; i <= diff; i++)
{
s += "0" ;
}
if (len_a < len_b)
{
a = s + a;
}
else
{
b = s + b;
}
var curr = 0;
var carry = 0;
var i = Math.max(len_a, len_b) - 1
while (i > -1)
{
curr = carry + parseInt(a[i]) + parseInt(b[i]);
carry = parseInt(curr / base);
curr %= base;
sums = String(curr) + sums;
i--;
}
if (carry > 0)
sums = String(carry) + sums;
return sums;
}
function convertBase(N)
{
let ans = "" ;
for ( var i = 0; i < N.length; i++)
{
ans = sumBaseB(ans, ans, 6);
ans = sumBaseB(ans, (N[i] == "0" ) ? "0" : "1" , 6);
}
return ans;
}
let N = "100111" ;
document.write(convertBase(N));
|
Time Complexity: O(len(N)2)
Auxiliary Space: O(len(N))
Note that the complexity of 1st approach is less than the second approach but the first approach can only handle binary integers up to 127 bits while the second approach can handle much larger values as well.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
01 Apr, 2022
Like Article
Save Article