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.
Examples :
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:

C++
// CPP program to count numbers of n digits // that are monotone. #include <cstring> #include <iostream> // Considering all possible digits as // {1, 2, 3, ..9} int static const DP_s = 9; int getNumMonotone(int len) { // DP[i][j] is going to store monotone // numbers of length i+1 considering // j+1 digits. int DP[len][DP_s]; memset(DP, 0, sizeof(DP)); // Unit length numbers for (int i = 0; i < DP_s; ++i) DP[0][i] = i + 1; // Single digit numbers for (int i = 0; i < len; ++i) DP[i][0] = 1; // Filling rest of the entries in bottom // up manner. for (int i = 1; i < len; ++i) for (int j = 1; j < DP_s; ++j) DP[i][j] = DP[i - 1][j] + DP[i][j - 1]; return DP[len - 1][DP_s - 1]; } // Driver code. int main() { std::cout << getNumMonotone(10); return 0; } |
Java
// Java program to count numbers // of n digits that are monotone. class GFG { // Considering all possible // digits as {1, 2, 3, ..9} static final int DP_s = 9; static int getNumMonotone(int len) { // DP[i][j] is going to store // monotone numbers of length // i+1 considering j+1 digits. int[][] DP = new int[len][DP_s]; // Unit length numbers for (int i = 0; i < DP_s; ++i) DP[0][i] = i + 1; // Single digit numbers for (int i = 0; i < len; ++i) DP[i][0] = 1; // Filling rest of the entries // in bottom up manner. for (int i = 1; i < len; ++i) for (int j = 1; j < DP_s; ++j) DP[i][j] = DP[i - 1][j] + DP[i][j - 1]; return DP[len - 1][DP_s - 1]; } // Driver code. public static void main (String[] args) { System.out.println(getNumMonotone(10)); } } // This code is contributed by Ansu Kumari. |
Python3
# Python3 program to count numbers of n # digits that are monotone. # Considering all possible digits as # {1, 2, 3, ..9} DP_s = 9 def getNumMonotone(ln): # DP[i][j] is going to store monotone # numbers of length i+1 considering # j+1 digits. DP = [[0]*DP_s for i in range(ln)] # Unit length numbers for i in range(DP_s): DP[0][i] = i + 1 # Single digit numbers for i in range(ln): DP[i][0] = 1 # Filling rest of the entries # in bottom up manner. for i in range(1, ln): for j in range(1, DP_s): DP[i][j] = DP[i - 1][j] + DP[i][j - 1] return DP[ln - 1][DP_s - 1] # Driver code print(getNumMonotone(10)) # This code is contributed by Ansu Kumari |
C#
// C# program to count numbers // of n digits that are monotone. using System; class GFG { // Considering all possible // digits as {1, 2, 3, ..9} static int DP_s = 9; static int getNumMonotone(int len) { // DP[i][j] is going to store // monotone numbers of length // i+1 considering j+1 digits. int[,] DP = new int[len,DP_s]; // Unit length numbers for (int i = 0; i < DP_s; ++i) DP[0,i] = i + 1; // Single digit numbers for (int i = 0; i < len; ++i) DP[i,0] = 1; // Filling rest of the entries // in bottom up manner. for (int i = 1; i < len; ++i) for (int j = 1; j < DP_s; ++j) DP[i,j] = DP[i - 1,j] + DP[i,j - 1]; return DP[len - 1,DP_s - 1]; } // Driver code. public static void Main () { Console.WriteLine(getNumMonotone(10)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to count numbers // of n digits that are monotone. function getNumMonotone($len) { // Considering all possible // digits as {1, 2, 3, ..9} $DP_s = 9; // DP[i][j] is going to store // monotone numbers of length // i+1 considering j+1 digits. $DP = array(array_fill(0, $len, 0), array_fill(0, $len, 0)); // Unit length numbers for ($i = 0; $i < $DP_s; ++$i) $DP[0][$i] = $i + 1; // Single digit numbers for ($i = 0; $i < $len; ++$i) $DP[$i][0] = 1; // Filling rest of the entries // in bottom up manner. for ($i = 1; $i < $len; ++$i) for ($j = 1; $j < $DP_s; ++$j) $DP[$i][$j] = $DP[$i - 1][$j] + $DP[$i][$j - 1]; return $DP[$len - 1][$DP_s - 1]; } // Driver code echo getNumMonotone(10); // This code is contributed by mits ?> |
Output :
43758
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.
Recommended Posts:
- Number of decimal numbers of length k, that are strict monotone
- 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
- Counting numbers whose difference from reverse is a product of k
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Counting Sort
- Counting pairs when a person can form pair with at most one
- Rencontres Number (Counting partial derangements)
- Sand Timer Flip Counting Problem
- Orbit counting theorem or Burnside's Lemma
- Counting Rock Samples | TCS Codevita 2020
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- 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
- Number of digits in the nth number made of given four digits
- 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
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

