Number of decimal numbers of length k, that are strict monotone
We 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 strict monotone. Number can’t start with 0.
Examples :
Input : 2 Output : 36 Numbers are 12, 13, ... 19, 23 24, ... 29, .... 89. Input : 3 Output : 84
Explanations of this problem follows the same rules as applied on:
Number of decimal numbers of length k, that are monotone
The only difference is that now we cannot take duplicates, so previously computed values are the one on the left and left top diagonal.
C++
// CPP program to count numbers of k // digits that are strictly monotone. #include <cstring> #include <iostream> int static const DP_s = 9; int getNumStrictMonotone(int len) { // DP[i][j] is going to store monotone // numbers of length i+1 considering // j+1 digits (1, 2, 3, ..9) 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; // Building dp[] in bottom up for (int i = 1; i < len; ++i) for (int j = 1; j < DP_s; ++j) DP[i][j] = DP[i - 1][j - 1] + DP[i][j - 1]; return DP[len - 1][DP_s - 1]; } // Driver code int main() { std::cout << getNumStrictMonotone(2); return 0; } |
Java
// Java program to count numbers of k // digits that are strictly monotone. import java.io.*; import java.util.*; class GFG { static int DP_s = 9; static int getNumStrictMonotone(int len) { // DP[i][j] is going to store monotone // numbers of length i+1 considering // j+1 digits (1, 2, 3, ..9) int[][] DP = new int[len][DP_s]; // Unit length numbers for (int i = 0; i < DP_s; ++i) DP[0][i] = i + 1; // Building dp[] in bottom up for (int i = 1; i < len; ++i) for (int j = 1; j < DP_s; ++j) DP[i][j] = DP[i - 1][j - 1] + DP[i][j - 1]; return DP[len - 1][DP_s - 1]; } public static void main(String[] args) { int n = 2; System.out.println(getNumStrictMonotone(n)); } } // This code is contributed by Gitanjali. |
Python3
# Python3 program to count numbers of k # digits that are strictly monotone. DP_s = 9 def getNumStrictMonotone(ln): # DP[i][j] is going to store monotone # numbers of length i+1 considering # j+1 digits (1, 2, 3, ..9) DP = [[0] * DP_s for _ in range(ln)] # Unit length numbers for i in range(DP_s): DP[0][i] = i + 1 # Building dp[] in bottom up for i in range(1, ln): for j in range(1, DP_s): DP[i][j] = DP[i - 1][j - 1] + DP[i][j - 1] return DP[ln - 1][DP_s - 1] # Driver code print(getNumStrictMonotone(2)) # This code is contributed by Ansu Kumari. |
C#
// C# program to count numbers of k // digits that are strictly monotone. using System; class GFG { static int DP_s = 9; static int getNumStrictMonotone(int len) { // DP[i][j] is going to store monotone // numbers of length i+1 considering // j+1 digits (1, 2, 3, ..9) int[,] DP = new int[len,DP_s]; // Unit length numbers for (int i = 0; i < DP_s; ++i) DP[0,i] = i + 1; // Building dp[] in bottom up for (int i = 1; i < len; ++i) for (int j = 1; j < DP_s; ++j) DP[i,j] = DP[i - 1,j - 1] + DP[i,j - 1]; return DP[len - 1,DP_s - 1]; } // Driver code public static void Main() { int n = 2; Console.WriteLine(getNumStrictMonotone(n)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to count // numbers of k digits // that are strictly // monotone. $DP_s = 9; function getNumStrictMonotone($len) { global $DP_s; // DP[i][j] is going to // store monotone numbers // of length i+1 considering // j+1 digits (1, 2, 3, ..9) $DP = array(array()); for($i = 0; $i < $len; $i++) { for($j = 0; $j < $DP_s; $j++) $DP[$i][$j] = 0; } // Unit length numbers for ($i = 0; $i < $DP_s; ++$i) $DP[0][$i] = $i + 1; // Building dp[] // in bottom up for ($i = 1; $i < $len; ++$i) for ($j = 1; $j < $DP_s; ++$j) $DP[$i][$j] = $DP[$i - 1][$j - 1] + $DP[$i][$j - 1]; return $DP[$len - 1][$DP_s - 1]; } // Driver code echo (getNumStrictMonotone(2)); // This code is contributed by // Manish Shaw(manishshaw1) ?> |
Output :
36
Recommended Posts:
- Counting numbers of n digits that are monotone
- Number of digits before the decimal point in the division of two numbers
- Find length of period in decimal value of 1/n
- Count number of digits after decimal on dividing a number
- 10's Complement of a decimal number
- 9's complement of a decimal number
- Exponential notation of a decimal number
- Check if the given decimal number has 0 and 1 digits only
- Sum of all odd length palindromic numbers within the range [L, R]
- Check if Decimal representation of an Octal number is divisible by 7
- Find a range of composite numbers of given length
- Converting Decimal Number lying between 1 to 3999 to Roman Numerals
- Print all increasing sequences of length k from first n natural numbers
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Given a number N in decimal base, find number of its digits in any base (base b)
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.
Improved By : manishshaw1



