Given a number N. Add one to the number in the first step and if the number has trailing zeros, remove all the trailing zeros in the second step. Continue the process for the next generated number. The task is to count the number of unique numbers that can be generated from these operations.
Input: N = 5
5 -> 6 -> 7 -> 8 -> 9 -> 1 -> 2 -> 3 -> 4 -> 5 (same sequence repeats)
Note that 10 is not included as it contained trailing zero
and removing the zero gave 1 as the next element.
Input: N = 28
Approach: The problem can be solved using recursion. Use a unordered_set to store all the unique numbers. In case a number is reached twice, we end the recursion as the same sequence will be repeated and we will not be getting any more unique numbers. Else insert the number to the set and in the first step increase the number by 1 and remove all trailing zeros in the next step if there are any.
Below is the implementation of the above approach:
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.
- Count number of trailing zeros in product of array
- Count number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*..
- Count number of trailing zeros in Binary representation of a number using Bitset
- Smallest number divisible by n and has at-least k trailing zeros
- Count different numbers that can be generated such that there digits sum is equal to 'n'
- Find the smallest number X such that X! contains at least Y trailing zeros.
- Number of trailing zeros in N * (N - 2) * (N - 4)*....
- Count array elements that can be maximized by adding any permutation of first N natural numbers
- Count numbers having N 0's and and M 1's with no leading zeros
- Maximum number of strings that can be formed with given zeros and ones
- Count of N-bit binary numbers without leading zeros
- Count ways to split a Binary String into three substrings having equal count of zeros
- Count trailing zeroes in factorial of a number
- Count of Array elements greater than or equal to twice the Median of K trailing Array elements
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Check if the given array can be reduced to zeros with the given operation performed given number of times
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- All possible numbers of N digits and base B without leading zeros
- Complete the sequence generated by a polynomial
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.