K 'th Boom Number | C ++ program to find K 'th Boom number ; This function uses queue data structure to K 'th Boom number ; Create an empty queue of strings ; Enqueue an empty string ; counter for K 'th element ; This loop checks the value of count to become equal to K when value of count will be equals to k we will print the Boom number ; current Boom number ; Store current Boom number before changing it ; Append "2" to string s1 and enqueue it ; check if count == k ; K 'th Boom number ; Append "3" to string s2 and enqueue it . Note that s2 contains the previous front ; check if count == k ; K 'th Boom number ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE 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 ; }