K’th Boom Number
Last Updated :
08 May, 2023
Boom numbers are numbers consisting only of digits 2 and 3. Given an integer k (0<k<=10^7) , display the k-th Boom number.
Examples:
Input : k = 2
Output: 3
Input : k = 3
Output: 22
Input : k = 100
Output: 322323
Input: k = 1000000
Output: 3332322223223222223
The idea is very simple like Generate Binary Numbers . Here also we use same approach ,
we use queue data structure to solve this problem. First enqueue “2” then “3” these two are first and second boom number respectively. Now set count=2, for each time pop() front of queue and append “2” in popped number and increment count++ if (count==k) then print current Boom number else append “3” in popped number and increment count++ if (count==k) then print current Boom number. Repeat the process until we reach to K’th Boom number.
This approach can be seen as BFS of a tree with root as empty string. Left child of every node has a 2 appended and right child has 3 appended.
Below is the implementation of this idea.
C++
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
void boomNumber(ll k)
{
queue<string> q;
q.push( "" );
ll count = 0;
while (count <= k)
{
string s1 = q.front();
q.pop();
string s2 = s1;
q.push(s1.append( "2" ));
count++;
if (count==k)
{
cout << s1 << endl;
break ;
}
q.push(s2.append( "3" ));
count++;
if (count==k)
{
cout << s2 << endl;
break ;
}
}
return ;
}
int main()
{
ll k = 1000000;
boomNumber(k);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static void boomNumber( long k)
{
Queue<String> q = new LinkedList<String>();
q.add( "" );
long count = 0 ;
while (count <= k)
{
String s1 = q.poll();
String s2 = s1;
q.add(s1+ "2" );
count++;
if (count==k)
{
System.out.println(s1);
break ;
}
q.add(s2+ "3" );
count++;
if (count==k)
{
System.out.println(s2);
break ;
}
}
return ;
}
public static void main(String args[])
{
long k = 1000000 ;
boomNumber(k);
}
}
|
Python3
def boomNumber(k):
q = []
q.append("")
count = 0
while (count < = k):
s1 = q[ 0 ]
q = q[ 1 :]
s2 = s1
s1 + = '2'
q.append(s1)
count = count + 1
if (count = = k):
print (s1)
break
s2 + = '3'
q.append(s2)
count = count + 1
if (count = = k):
print (s2)
break
return
k = 1000000
boomNumber(k)
|
C#
using System;
using System.Collections;
class GFG{
static void boomNumber( long k)
{
Queue q = new Queue();
q.Enqueue( "" );
long count = 0;
while (count <= k)
{
string s1 = ( string )q.Dequeue();
string s2 = s1;
s1 += "2" ;
q.Enqueue(s1);
count++;
if (count == k)
{
Console.Write(s1);
break ;
}
s2 += "3" ;
q.Enqueue(s2);
count++;
if (count == k)
{
Console.Write(s2);
break ;
}
}
return ;
}
public static void Main( string []arg)
{
long k = 1000000;
boomNumber(k);
}
}
|
Javascript
<script>
function boomNumber(k){
let q = []
q.push( "" )
let count = 0
while (count <= k){
let s1 = q.shift()
let s2 = s1
s1 += '2 '
q.push(s1)
count = count + 1
// check if count==k
if (count==k){
document.write(s1,"</br>") // K' th Boom number
break
}
s2 += '3'
q.push(s2)
count = count + 1
if (count==k){
document.write(s2, "</br>" )
break
}
}
return
}
let k = 1000000
boomNumber(k)
</script>
|
Output
3332322223223222223
Time Complexity: O(K)
Auxiliary Space: O(n)
This article is reviewed by team GeeksforGeeks.
Share your thoughts in the comments
Please Login to comment...