Skip to content
Related Articles

Related Articles

Improve Article

Count N-digit numbers that contains every possible digit atleast once

  • Difficulty Level : Hard
  • Last Updated : 08 Jul, 2021

Given a positive integer N, the task is to count the number of N-digit numbers such that every digit from [0-9] occurs at least once.  

 Examples : 

Input: N = 10
Output : 3265920

Input: N = 5
Output: 0
Explanation: Since the number of digits is less than the minimum number of digits required [0-9], the answer is 0.

Naive Approach: The simplest approach to solve the problem is to generate over all possible N-digit numbers and for each such number, check if all its digits satisfy the required condition or not.
Time Complexity: O(10N*N)
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach, the idea is to use Dynamic Programming as it has overlapping subproblems and optimal substructure. The subproblems can be stored in dp[][] table using memoization, where dp[digit][mask] stores the answer from the digitth position till the end, when the included digits are represented using a mask. 



Follow the steps below to solve this problem:

  • Define a recursive function, say countOfNumbers(digit, mask), and perform the following steps:
    • Base Case: If the value of digit is equal to N+1, then check if the value of the mask is equal to (1 << 10 – 1). If found to be true, return 1 as a valid N-digit number is formed.
    • If the result of the state dp[digit][mask] is already computed, return this state dp[digit][mask].
    • If the current position is 1, then any digit from [1-9] can be placed. If N is equal to 1, then 0 can be placed as well.
    • For any other position, any digit from [0-9] can be placed.
    • If a particular digit ‘i’ is included, then update mask as mask = mask | (1 << i ).
    • After making a valid placement, recursively call the countOfNumbers function for index digit+1.
    • Return the sum of all possible valid placements of digits as the answer.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Stores the dp-states
long long dp[100][1 << 10];
 
// Function to calculate the count of
// N-digit numbers that contains all
// digits from [0-9] atleast once
long long countOfNumbers(int digit,
                         int mask, int N)
{
    // If all digits are traversed
    if (digit == N + 1) {
 
        // Check if all digits are
        // included in the mask
        if (mask == (1 << 10) - 1)
            return 1;
        return 0;
    }
 
    long long& val = dp[digit][mask];
 
    // If the state has
    // already been computed
    if (val != -1)
        return val;
 
    val = 0;
 
    // If the current digit is 1, any
    // digit from [1-9] can be placed.
    // If N==1, 0 can also be placed
    if (digit == 1) {
        for (int i = (N == 1 ? 0 : 1); i <= 9; ++i) {
 
            val += countOfNumbers(digit + 1,
                                  mask | (1 << i), N);
        }
    }
 
    // For other positions, any digit
    // from [0-9] can be placed
    else {
        for (int i = 0; i <= 9; ++i) {
 
            val += countOfNumbers(digit + 1,
                                  mask | (1 << i), N);
        }
    }
 
    // Return the answer
    return val;
}
 
// Driver Code
int main()
{
    // Initializing dp array with -1.
    memset(dp, -1, sizeof dp);
 
    // Given Input
    int N = 10;
 
    // Function Call
    cout << countOfNumbers(1, 0, N);
 
    return 0;
}

Javascript




<script>
 
// JavaScript program for the above approach
 
// Stores the dp-states
let dp = new Array(100);
for (let i = 0; i < 100; i++) {
    dp[i] = new Array(1 << 10).fill(-1);
}
 
// Function to calculate the count of
// N-digit numbers that contains all
// digits from [0-9] atleast once
function countOfNumbers(digit, mask, N)
{
    // If all digits are traversed
    if (digit == N + 1) {
 
        // Check if all digits are
        // included in the mask
        if (mask == (1 << 10) - 1)
            return 1;
        return 0;
    }
 
    let val = dp[digit][mask];
 
    // If the state has
    // already been computed
    if (val != -1)
        return val;
 
    val = 0;
 
    // If the current digit is 1, any
    // digit from [1-9] can be placed.
    // If N==1, 0 can also be placed
    if (digit == 1) {
        for (let i = (N == 1 ? 0 : 1); i <= 9; ++i) {
 
            val += countOfNumbers(digit + 1,
                                  mask | (1 << i), N);
        }
    }
 
    // For other positions, any digit
    // from [0-9] can be placed
    else {
        for (let i = 0; i <= 9; ++i) {
 
            val += countOfNumbers(digit + 1,
                                  mask | (1 << i), N);
        }
    }
 
    // Return the answer
    return val;
}
 
// Driver Code
 
    // Given Input
    let N = 10;
 
    // Function Call
    document.write(countOfNumbers(1, 0, N));
     
</script>

 
 

Output: 
3265920

 

 

Time Complexity: O(N2*210)
Auxiliary Space: O(N*210)

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :