Given a number N, the task is to print the string of ‘A’ and ‘B’ corresponding to that number.
If we represent all numbers as a string of ‘A’ and ‘B’ as follows,
1 = A
2 = B
3 = AA
4 = AB
5 = BA
6 = BB
7 = AAA
8 = AAB
9 = ABA
10 = ABB
.....
.....
1000000 = BBBABAAAABAABAAAAAB
Examples:
Input: N = 30
Output: BBBB
Input: N = 55
Output: BBAAA
Input: N = 100
Output: BAABAB
Approach:
- This representation is a little bit related to binary representation.
- First, we have to find the number of characters required to print the string corresponding to the given number, That is the length of the resultant string.
- There are 2 numbers of length 1, 4 numbers of length 2, 8 numbers of length 3 and so on…
- Therefore, k length string of ‘A’ and ‘B’ can represent pow(2, k) numbers from (pow(2, k)-2)+1 to pow(2, k+1)-2, that is AA…A (k times) to BB…B (k times).
- Therefore, for printing the corresponding string, first, calculate the length of the string, let it be k. Now calculate,
N = M - (pow(2, k)-2);
- Now run the following loop to print the corresponding string.
while (k>0)
{
num = pow(2, k-1);
if (num >= N)
cout <<"A";
else{
cout <<"B";
N -= num;
}
k--;
}
Below is the implementation of the above approach:
C++
#include <cmath>
#include <iostream>
using namespace std;
int no_of_characters( int M)
{
int k = 1;
while ( true ) {
if ( pow (2, k + 1) - 2 < M)
k++;
else
break ;
}
return k;
}
void print_string( int M)
{
int k, num, N;
k = no_of_characters(M);
N = M - ( pow (2, k) - 2);
while (k > 0) {
num = pow (2, k - 1);
if (num >= N)
cout << "A" ;
else {
cout << "B" ;
N -= num;
}
k--;
}
cout << endl;
}
int main()
{
int M;
M = 30;
print_string(M);
M = 55;
print_string(M);
M = 100;
print_string(M);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int no_of_characters( int M)
{
int k = 1 ;
while ( true )
{
if (( int )Math.pow( 2 , k + 1 ) - 2 < M)
k++;
else
break ;
}
return k;
}
static void print_string( int M)
{
int k, num, N;
k = no_of_characters(M);
N = M - (( int )Math.pow( 2 , k) - 2 );
while (k > 0 )
{
num = ( int )Math.pow( 2 , k - 1 );
if (num >= N)
System.out.print( "A" );
else
{
System.out.print( "B" );
N -= num;
}
k--;
}
System.out.println();
}
public static void main(String args[])
{
int M;
M = 30 ;
print_string(M);
M = 55 ;
print_string(M);
M = 100 ;
print_string(M);
}
}
|
Python3
from math import pow
def no_of_characters(M):
k = 1
while ( True ):
if ( pow ( 2 , k + 1 ) - 2 < M):
k + = 1
else :
break
return k
def print_string(M):
k = no_of_characters(M)
N = M - ( pow ( 2 , k) - 2 )
while (k > 0 ):
num = pow ( 2 , k - 1 )
if (num > = N):
print ( "A" , end = "")
else :
print ( "B" , end = "")
N - = num
k - = 1
print ( "\n" , end = "")
if __name__ = = '__main__' :
M = 30 ;
print_string(M)
M = 55
print_string(M)
M = 100
print_string(M)
|
C#
using System;
class GFG
{
static int no_of_characters( int M)
{
int k = 1;
while ( true )
{
if (( int )Math.Pow(2, k + 1) - 2 < M)
k++;
else
break ;
}
return k;
}
static void print_string( int M)
{
int k, num, N;
k = no_of_characters(M);
N = M - (( int )Math.Pow(2, k) - 2);
while (k > 0)
{
num = ( int )Math.Pow(2, k - 1);
if (num >= N)
Console.Write( "A" );
else
{
Console.Write( "B" );
N -= num;
}
k--;
}
Console.WriteLine();
}
public static void Main()
{
int M;
M = 30;
print_string(M);
M = 55;
print_string(M);
M = 100;
print_string(M);
}
}
|
PHP
<?php
function no_of_characters( $M )
{
$k = 1;
while (true)
{
if (pow(2, $k + 1) - 2 < $M )
$k ++;
else
break ;
}
return $k ;
}
function print_string( $M )
{
$k ; $num ; $N ;
$k = no_of_characters( $M );
$N = $M - (pow(2, $k ) - 2);
while ( $k > 0)
{
$num = pow(2, $k - 1);
if ( $num >= $N )
echo "A" ;
else {
echo "B" ;
$N -= $num ;
}
$k --;
}
echo "\n" ;
}
$M ;
$M = 30;
print_string( $M );
$M = 55;
print_string( $M );
$M = 100;
print_string( $M );
?>
|
Javascript
<script>
function no_of_characters( M){
let k = 1;
while ( true ) {
if (Math.pow(2, k + 1) - 2 < M)
k++;
else
break ;
}
return k;
}
function print_string( M)
{
let k, num, N;
k = no_of_characters(M);
N = M - (Math.pow(2, k) - 2);
while (k > 0) {
num = Math.pow(2, k - 1);
if (num >= N)
document.write( "A" );
else {
document.write( "B" );
N -= num;
}
k--;
}
document.write( "<br>" );
}
let M;
M = 30;
print_string(M);
M = 55;
print_string(M);
M = 100;
print_string(M);
</script>
|
Output:
BBBB
BBAAA
BAABAB