Given an integer n > 0, which denotes the number of digits, the task to find total number of n-digit positive integers which are non-decreasing in nature.
A non-decreasing integer is a one in which all the digits from left to right are in non-decreasing form. ex: 1234, 1135, ..etc.
Note :Leading zeros also count in non-decreasing integers such as 0000, 0001, 0023, etc are also non-decreasing integers of 4-digits.
Input : n = 1 Output : 10 Numbers are 0, 1, 2, ...9. Input : n = 2 Output : 55 Input : n = 4 Output : 715
Naive Approach : We generate all possible n-digit numbers and then for each number we check whether it is non-decreasing or not.
Time Complexity : (n*10^n), where 10^n is for generating all possible n-digits number and n is for checking whether a particular number is non-decreasing or not.
Dynamic Programming :
If we fill digits one by one from left to right, following conditions hold.
- If current last digit is 9, we can fill only 9s in remaining places. So only one solution is possible if current last digit is 9.
- If current last digit is less than 9, then we can recursively compute count using following formula.
a[i][j] = a[i-1][j] + a[i][j + 1] For every digit j smaller than 9. We consider previous length count and count to be increased by all greater digits.
We build a matrix a where a[i][j] = count of all valid i-digit non-decreasing integers with j or greater than j as the leading digit. The solution is based on below observations. We fill this matrix column-wise, first calculating a then using this value to compute a and so on.
At any instant if we wish to calculate a[i][j] means number of i-digits non-decreasing integers with leading digit as j or digit greater than j, we should add up a[i-1][j] (number of i-1 digit integers which should start from j or greater digit, because in this case if we place j as its left most digit then our number will be i-digit non-decreasing number) and a[i][j+1] (number of i-digit integers which should start with digit equals to greater than j+1). So, a[i][j] = a[i-1][j] + a[i][j+1].
Non-decreasing digits = 55
Time Complexity : O(10*n) equivalent to O(n).
This article is contributed by Shivam Pradhan (anuj_charm). 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Number of decimal numbers of length k, that are strict monotone
- Counting numbers of n digits that are monotone
- Different ways to sum n using numbers greater than or equal to m
- Number of ways to arrange N items under given constraints
- Unique paths in a Grid with Obstacles
- Convert to Strictly increasing array with minimum changes
- Count of Palindromic substrings in an Index range
- Find maximum possible stolen value from houses
- Painting Fence Algorithm
- Bitmasking and Dynamic Programming | Set-2 (TSP)
- Find the Longest Increasing Subsequence in Circular manner
- Choose maximum weight with given weight and value ratio
- Counts paths from a point to reach Origin
- Ways to sum to N using array elements with repetition allowed
- 0-1 Knapsack Problem | DP-10