Number of decimal numbers of length k, that are strict monotone

We call decimal number a monotone if:D[i] \leq D[i+1], 0 \leq i \leq |D|

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Improved By : manishshaw1




Recommended Posts:



2.3 Average Difficulty : 2.3/5.0
Based on 3 vote(s)






User Actions