Count numbers less than N containing digits from the given set : Digit DP
Given an integer N and set of digits D[], which consists of digits from [1, 9]. The task is to count the numbers possible less than N, whose digits are from the given set of digits.
Examples:
Input: D = [“1”, “4”, “9”], N = 10
Output: 3
Explanation:
There are only 3 numbers possible less than 3 with given set of digits –
1, 4, 9
Input: D[] = {“1”, “3”, “5”, “7”}, N = 100
Output: 20
Explanation:
There are only 20 numbers possible less than 100 with given set of digits –
1, 3, 5, 7, 11, 13, 15, 17, 31, 33, 35, 37, 51, 53, 55, 57, 71, 73, 75, 77.
Naive Approach:Check the digits of all the numbers of range [1, N], If all the digits of a number belong to the given digit set then increment the count by 1.
Efficient Approach: The idea is to use the concept of Digit DP and traverse the given set of digits and generate all the numbers which are strictly less than the given number N. Recursively choose the digit for all the possible position of the number and pass a boolean variable tight to check that by including that digit, the number falls into the given range or not.
Let’s think of the possible state for the DP –
- pos: It tells about the position of the digit to be chosen, such that the number falls into the given range.
- tight: This will help us know about the current digits are restricted or not. If the digits are restricted, then any digit can be choosen from the given set of digits. Otherwise, the digits can be chosen in range [1, N[pos]].
- size: It will tells the number of the digits to be chosen.
Below is the illustration of the recursive function:
- Base Case: The base case for this problem will be when the position of the digit to be chosen is equal to the length of digits to be chosen, then there is only one possible number containing the digits which are chosen till yet.
if (position == countDigits(N)) return 1 - Recursive Case: For generating the number in the given range, use the tight variable to choose the possible digits in range as follows:
- If the value of tight is 0, denotes that by including that digit will give the number less than the given range.
- Otherwise, If the value of tight is 1, denotes that by including that digit, it will give the number greater than the given range. So we can remove all permutations after getting tight value 1 to avoid more number of recursive calls.
- Store the count of the numbers possible after choosing each digit for every position.
Below is the implementation of the above approach:
C++
// C++ implmentation to find the // count of numbers possible less // than N, such that every digit // is from the given set of digits #include <bits/stdc++.h> using namespace std; int dp[15][2]; // Function to convert integer // into the string string convertToString(int num) { stringstream ss; ss << num; string s = ss.str(); return s; } // Recursive function to find the // count of numbers possible less // than N, such that every digit // is from the given set of digits int calculate(int pos, int tight, int D[], int sz, string& num) { // Base case if (pos == num.length()) return 1; // Condition when the subproblem // is computed previously if (dp[pos][tight] != -1) return dp[pos][tight]; int val = 0; // Condition when the number // chosen till now is definietly // smaller than the given number N if (tight == 0) { // Loop to traverse all the // digits of the given set for (int i = 0; i < sz; i++) { if (D[i] < (num[pos] - '0')) { val += calculate(pos + 1, 1, D, sz, num); } else if (D[i] == num[pos] - '0') val += calculate(pos + 1, tight, D, sz, num); } } else { // Loop to traverse all the // digits from the given set for (int i = 0; i < sz; i++) { val += calculate(pos + 1, tight, D, sz, num); } } // Store the solution for // current subproblem return dp[pos][tight] = val; } // Function to count the numbers // less then N from given set of digits int countNumbers(int D[], int N, int sz) { // Converting the number to string string num = convertToString(N); int len = num.length(); // Intially no subproblem // is solved till now memset(dp, -1, sizeof(dp)); // Find the solution of all the // number equal to the length of // the given number N int ans = calculate(0, 0, D, sz, num); // Loop to find the number less in // in the length of the given number for (int i = 1; i < len; i++) ans += calculate(i, 1, D, sz, num); return ans; } // Driver Code int main() { int sz = 3; int D[sz] = { 1, 4, 9 }; int N = 10; // Function Call cout << countNumbers(D, N, sz); return 0; } |
3
Time complexity: O(Len(D))
Space complexity: O(12*2)
Recommended Posts:
- Count of n digit numbers whose sum of digits equals to given sum
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Count of Numbers in Range where first digit is equal to last digit of the number
- N digit numbers divisible by 5 formed from the M digits
- 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 numbers from 1 to n that have 4 as a digit
- Count numbers (smaller than or equal to N) with given digit sum
- Count numbers whose maximum sum of distinct digit-sum is less than or equals M
- Count of numbers in range which are divisible by M and have digit D at odd places
- Count of N-digit numbers in base K with no two consecutive zeroes
- Count of Numbers in a Range where digit d occurs exactly K times
- Count of Numbers in a Range divisible by m and having digit d in even positions
- 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
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.

