Given a range represented by two positive integers L and R and a positive integer K. Find the count of numbers in the range where the number does not contain more than K non zero digits.

**Examples:**

Input :L = 1, R = 1000, K = 3Output :1000Explanation :All the numbers from 1 to 1000 are 3 digit numbers which obviously cannot contain more than 3 non zero digits.Input :L = 9995, R = 10005Output :6Explanation :Required numbers are 10000, 10001, 10002, 10003, 10004 and 10005

Prerequisites : Digit DP

There can be two approaches to solve this type of problem, one can be a combinatorial solution and other can be a dynamic programming based solution. Below is a detailed approach of solving this problem using a digit dynamic programming approach.

**Dynamic Programming Solution :** Firstly, if we are able to count the required numbers upto R i.e. in the range [0, R], we can easily reach our answer in the range [L, R] by solving for from zero to R and then subtracting the answer we get after solving for from zero to L – 1. Now, we need to define the DP states.

**DP States**:

- Since we can consider our number as a sequence of digits, one state is the
at which we are currently in. This position can have values from 0 to 18 if we are dealing with the numbers upto 10*position*^{18}. In each recursive call, we try to build the sequence from left to right by placing a digit from 0 to 9. - Second state is the
which defines the number of non zero digits, we have placed in the number we are trying to build.*count* - Another state is the boolean variable
which tells the number we are trying to build has already become smaller than R, so that in the upcoming recursive calls we can place any digit from 0 to 9. If the number has not become smaller, maximum limit of digit we can place is digit at current position in R.*tight*

In the final recursive call, when we are at the last position if the count of non zero digits is less than or equal to K, return 1 otherwise return 0.

Below is the implementation of the above approach.

## C++

`// CPP Program to find the count of ` `// numbers in a range where the number ` `// does not contain more than K non ` `// zero digits ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `const` `int` `M = 20; ` ` ` `// states - position, count, tight ` `int` `dp[M][M][2]; ` ` ` `// K is the number of non zero digits ` `int` `K; ` ` ` `// This function returns the count of ` `// required numbers from 0 to num ` `int` `countInRangeUtil(` `int` `pos, ` `int` `cnt, ` `int` `tight, ` ` ` `vector<` `int` `> num) ` `{ ` ` ` `// Last position ` ` ` `if` `(pos == num.size()) { ` ` ` `// If count of non zero digits ` ` ` `// is less than or equal to K ` ` ` `if` `(cnt <= K) ` ` ` `return` `1; ` ` ` `return` `0; ` ` ` `} ` ` ` ` ` `// If this result is already computed ` ` ` `// simply return it ` ` ` `if` `(dp[pos][cnt][tight] != -1) ` ` ` `return` `dp[pos][cnt][tight]; ` ` ` ` ` `int` `ans = 0; ` ` ` ` ` `// Maximum limit upto which we can place ` ` ` `// digit. If tight is 1, means number has ` ` ` `// already become smaller so we can place ` ` ` `// any digit, otherwise num[pos] ` ` ` `int` `limit = (tight ? 9 : num[pos]); ` ` ` ` ` `for` `(` `int` `dig = 0; dig <= limit; dig++) { ` ` ` `int` `currCnt = cnt; ` ` ` ` ` `// If the current digit is nonzero ` ` ` `// increment currCnt ` ` ` `if` `(dig != 0) ` ` ` `currCnt++; ` ` ` ` ` `int` `currTight = tight; ` ` ` ` ` `// At this position, number becomes ` ` ` `// smaller ` ` ` `if` `(dig < num[pos]) ` ` ` `currTight = 1; ` ` ` ` ` `// Next recursive call ` ` ` `ans += countInRangeUtil(pos + 1, currCnt, ` ` ` `currTight, num); ` ` ` `} ` ` ` `return` `dp[pos][cnt][tight] = ans; ` `} ` ` ` `// This function converts a number into its ` `// digit vector and uses above function to compute ` `// the answer ` `int` `countInRange(` `int` `x) ` `{ ` ` ` `vector<` `int` `> num; ` ` ` `while` `(x) { ` ` ` `num.push_back(x % 10); ` ` ` `x /= 10; ` ` ` `} ` ` ` `reverse(num.begin(), num.end()); ` ` ` ` ` `// Initialize dp ` ` ` `memset` `(dp, -1, ` `sizeof` `(dp)); ` ` ` `return` `countInRangeUtil(0, 0, 0, num); ` `} ` ` ` `// Driver Code to test above functions ` `int` `main() ` `{ ` ` ` `int` `L = 1, R = 1000; ` ` ` `K = 3; ` ` ` `cout << countInRange(R) - countInRange(L - 1) << endl; ` ` ` ` ` `L = 9995, R = 10005, K = 2; ` ` ` `cout << countInRange(R) - countInRange(L - 1) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find the count of ` `// numbers in a range where the number ` `// does not contain more than K non ` `// zero digits ` `import` `java.util.*; ` `class` `Solution ` `{ ` `static` `final` `int` `M = ` `20` `; ` ` ` `// states - position, count, tight ` `static` `int` `dp[][][]= ` `new` `int` `[M][M][` `2` `]; ` ` ` `// K is the number of non zero digits ` `static` `int` `K; ` `static` `Vector<Integer> num; ` ` ` `// This function returns the count of ` `// required numbers from 0 to num ` `static` `int` `countInRangeUtil(` `int` `pos, ` `int` `cnt, ` `int` `tight ) ` `{ ` ` ` `// Last position ` ` ` `if` `(pos == num.size()) { ` ` ` `// If count of non zero digits ` ` ` `// is less than or equal to K ` ` ` `if` `(cnt <= K) ` ` ` `return` `1` `; ` ` ` `return` `0` `; ` ` ` `} ` ` ` ` ` `// If this result is already computed ` ` ` `// simply return it ` ` ` `if` `(dp[pos][cnt][tight] != -` `1` `) ` ` ` `return` `dp[pos][cnt][tight]; ` ` ` ` ` `int` `ans = ` `0` `; ` ` ` ` ` `// Maximum limit upto which we can place ` ` ` `// digit. If tight is 1, means number has ` ` ` `// already become smaller so we can place ` ` ` `// any digit, otherwise num[pos] ` ` ` `int` `limit = (tight!=` `0` `? ` `9` `: num.get(pos)); ` ` ` ` ` `for` `(` `int` `dig = ` `0` `; dig <= limit; dig++) { ` ` ` `int` `currCnt = cnt; ` ` ` ` ` `// If the current digit is nonzero ` ` ` `// increment currCnt ` ` ` `if` `(dig != ` `0` `) ` ` ` `currCnt++; ` ` ` ` ` `int` `currTight = tight; ` ` ` ` ` `// At this position, number becomes ` ` ` `// smaller ` ` ` `if` `(dig < num.get(pos)) ` ` ` `currTight = ` `1` `; ` ` ` ` ` `// Next recursive call ` ` ` `ans += countInRangeUtil(pos + ` `1` `, currCnt, currTight); ` ` ` `} ` ` ` `return` `dp[pos][cnt][tight] = ans; ` `} ` ` ` `// This function converts a number into its ` `// digit vector and uses above function to compute ` `// the answer ` `static` `int` `countInRange(` `int` `x) ` `{ ` ` ` `num= ` `new` `Vector<Integer>(); ` ` ` `while` `(x!=` `0` `) { ` ` ` `num.add(x % ` `10` `); ` ` ` `x /= ` `10` `; ` ` ` `} ` ` ` `Collections.reverse(num); ` ` ` ` ` `// Initialize dp ` ` ` `for` `(` `int` `i=` `0` `;i<M;i++) ` ` ` `for` `(` `int` `j=` `0` `;j<M;j++) ` ` ` `for` `(` `int` `k=` `0` `;k<` `2` `;k++) ` ` ` `dp[i][j][k]=-` `1` `; ` ` ` `return` `countInRangeUtil(` `0` `, ` `0` `, ` `0` `); ` `} ` ` ` `// Driver Code to test above functions ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `L = ` `1` `, R = ` `1000` `; ` ` ` `K = ` `3` `; ` ` ` `System.out.println( countInRange(R) - countInRange(L - ` `1` `) ); ` ` ` ` ` `L = ` `9995` `; R = ` `10005` `; K = ` `2` `; ` ` ` `System.out.println( countInRange(R) - countInRange(L - ` `1` `) ); ` `} ` `} ` `//contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

**Output:**

1000 6

**Time Complexity : **O(18 * 18 * 2 * 10), if we are dealing with the numbers upto 10^{18}

## Recommended Posts:

- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count numbers in range such that digits in it and it's product with q are unequal
- Count Numbers with N digits which consists of odd number of 0's
- Count Numbers with N digits which consists of even number of 0’s
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Program to find count of numbers having odd number of divisors in given range
- Total numbers with no repeated digits in a range
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count numbers with same first and last digits
- Count Unary Numbers in a Range
- Count factorial numbers in a given range
- Count the numbers divisible by 'M' in a given range
- Count different numbers that can be generated such that there digits sum is equal to 'n'
- Count of n digit numbers whose sum of digits equals to given sum
- Count numbers formed by given two digit with sum having given 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 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.