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.
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 poped number and increment count++ if (count==k) then print current Boom number else append “3” in poped number and increment count++ if (count==k) then print current Boom number. Repeat the process untill 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 C++ implementation of this idea.
This article is contributed by Shashank Mishra (Gullu). This article is reviewed by team GeeksforGeeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Count number of triplets with product equal to given number with duplicates allowed
- Count number of trailing zeros in Binary representation of a number using Bitset
- Maximum number formed from array with K number of adjacent swaps allowed
- Find minimum number to be divided to make a number a perfect square
- Number of ways to split a binary number such that every part is divisible by 2
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Maximum number of contiguous array elements with same number of set bits
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Number of times the largest perfect square number can be subtracted from N
- Find a number which give minimum sum when XOR with every number of array of integers
- Given number of matches played, find number of teams in tournament
- Check if a number is divisible by all prime divisors of another number
- Count number of triplets with product equal to given number
- Program to calculate the number of odd days in given number of years
- Find the number of jumps to reach X in the number line from zero