Given a number n and we have to find n-th number such that it’s digits only consist 0, 1, 2, 3, 4 or 5.
Input: n = 6 Output: 5 Input: n = 10 Output: 13
We first store 0, 1, 2, 3, 4, 5 in an array. We can see that next numbers will be 10, 11, 12,,13, 14, 15 and after that numbers will be 20, 21, 23, 24, 25 and so on. We can see the pattern that is repeating again and again. We save the calculated result and use it for further calculations.
next 6 numbers are-
1*10+0 = 10
1*10+1 = 11
1*10+2 = 12
1*10+3 = 13
1*10+4 = 14
1*10+5 = 15
and after that next 6 numbers will be-
2*10+0 = 20
2*10+1 = 21
2*10+2 = 22
2*10+3 = 23
2*10+4 = 24
2*10+5 = 25
We use this pattern to find the n-th number. Below is complete algorithm.
1) push 0 to 5 in ans vector 2) for i=0 to n for j=0 to 6 // this will be the case when first // digit will be zero if (ans[i]*10! = 0) ans.push_back(ans[i]*10 + ans[j]) 3) print ans[n-1]
Efficient Method :
1. First convert number n to base 6.
2. Store the converted value simultaneously in an array.
3. Print that array in reverse order.
Below is the implementation of the above algorithm:
Another Efficient Method:
1. Decrease the number N by 1.
2. Convert the number N to base 6.
Below is the implementation of the above algorithm :
Time Complexity: O(logN)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Number of digits in the nth number made of given four digits
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum digits to be removed to make either all digits or alternating digits same
- Program to find last two digits of Nth Fibonacci number
- Find Nth even length palindromic number formed using digits X and Y
- Nth number made up of odd digits only
- Nth positive number whose absolute difference of adjacent digits is at most 1
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Smallest number with given sum of digits and sum of square of digits
- Minimum number of digits to be removed so that no two consecutive digits are same
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Find the average of k digits from the beginning and l digits from the end of the given number
- Check if the sum of digits of number is divisible by all of its digits
- Sum of the digits of square of the given number which has only 1's as its digits