Call decimal number a monotone if:
Write a program which takes positive number n on input and returns number of decimal numbers of length n that are monotone. Numbers can’t start with 0.
Input : 1 Output : 9 Numbers are 1, 2, 3, ... 9 Input : 2 Output : 45 Numbers are 11, 12, 13, .... 22, 23 ...29, 33, 34, ... 39. Count is 9 + 8 + 7 ... + 1 = 45
Explanation: Let’s start by example of monotone numbers:
All those numbers are monotone as each digit on higher place is than the one before it.
What are the monotone numbers are of length 1 and digits 1 or 2? It is question to ask yourself at the very beginning. We can see that possible numbers are:
That was easy, now lets expand the question to digits 1, 2 and 3:
Now different question, what are the different monotone numbers consisting of only 1 and length 3 are there?
Lets try now draw this very simple observation in 2 dimensional array for number of length 3, where first column is the length of string and first row is possible digits:
Let’s try to fill 3rd row 3rd column(number of monotone numbers consisting from numbers 1 or 2 with length 2). This should be:
If we will look closer we already have subsets of this set i.e:
– Monotone numbers that has length 2 and consist of 1 or 2
– Monotone numbers of length 2 and consisting of number 2
We just need to add previous values to get the longer one.
Final matrix should look like this:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Number of decimal numbers of length k, that are strict monotone
- Counting numbers whose difference from reverse is a product of k
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Sort the numbers according to their sum of digits
- Compute sum of digits in all numbers from 1 to n
- Count numbers with same first and last digits
- Find the numbers from 1 to N that contains exactly k non-zero digits
- Sum of the products of same placed digits of two numbers
- Print numbers with digits 0 and 1 only such that their sum is N
- Count of numbers from range [L, R] that end with any of the given digits
- Count numbers less than N containing digits from the given set : Digit DP
- Count of numbers from range [L, R] whose sum of digits is Y
- Check if two numbers have same number of digits
- Count of numbers with all digits same in a given range
- Number of digits in the product of two numbers
- Sum of all numbers that can be formed with permutations of n digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.