Find the number of digits in the nth number constructed by using 6, 1, 4 and 9 as the only digits in the ascending order.
First few numbers constructed by using only 6, 1, 4 and 9 as digits in the ascending order would be: 1, 6, 4,
9, 11, 14, 16, 19, 41, 44, 46, 49, 61, 64, 66, 69, 91, 94, 96, 99, 111, 114, 116, 119 and so on.
Input : 6 Output : 2 6th digit of the series is 14 which has 2 digits. Input : 21 Output : 3 21st digit of the series is 111 which has 3 digits.
Simple Approach: This is a brute force approach.
1. Initialize a number to 1 and a counter to 0.
2. Check if the initialized number has only 6, 1, 4 or 9 as it’s digits.
3. If it has only the mentioned digits then increase the counter by 1.
4. Increase the number and repeat the above steps until the counter is less than n.
Note: The value of n could be large and hence this approach can’t work as it’s not time efficient.
Efficient Approach: You can calculate the number of k digit numbers in O (1) time and they will be always be power of 4, for instance number of 1 digit numbers in the series would be 4, number of 2 digit numbers in the series would be 16 and so on.
1. Count all subsequent k digit numbers and keep adding them to a sum.
2. Break the loop when sum is greater than or equal to n.
3. Maintain a counter to keep track of the number of digits.
4. The value of the counter at the break of the loop will indicate the answer.
Note: Since n could be really large we have used boost library, to know more about boost library give this article a read: Advanced C++ with boost library
- Nth number made up of odd digits only
- Sum of nth terms of Modified Fibonacci series made by every pair of two arrays
- Smallest multiple of a given number made of digits 0 and 9 only
- Maximum possible time that can be formed from four digits
- Find the n-th number made of even digits only
- Finding n-th number made of prime digits (2, 3, 5 and 7) only
- Minimum number closest to N made up of odd digits only
- Check if given array can be made 0 with given operations performed any number of times
- How to check if given four points form a square
- Minimum sum obtained from groups of four elements from the given array
- Find the prime P using given four integers
- Find the number of ways to divide number into four parts such that a = c and b = d
- Queries to find whether a number has exactly four distinct factors or not
- Maximize the product of four factors of a Number
- Number of ways N can be divided into four parts to construct a rectangle
- Find position of the given number among the numbers made of 4 and 7
- Number of words that can be made using exactly P consonants and Q vowels from the given string
- Check if X and Y can be made zero by using given operation any number of times
- Check if four segments form a rectangle
- Euler's Four Square Identity
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.