Count of N digit numbers which contains all single digit primes
Given a positive integer N, the task is to count the number of N-digit numbers which contain all single digit primes.
Examples:
Input: N = 4
Output: 24
Explanation: The number of single digit primes is 4 i.e.{2, 3, 5, 7}. Hence number of ways to arrange 4 numbers in 4 places is 4! = 24.Input: N = 5
Output: 936
Naive Approach: The simplest approach to solve the given problem is to generate all possible N-digit numbers and count those numbers which contain all single digit prime numbers. After checking for all the numbers, print the value of the count as the resultant total count of numbers.
Time Complexity: O(N *10N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by using Dynamic Programming because it has overlapping subproblems and optimal substructure. The subproblems can be stored in dp[][] table using memoization where dp[index][mask] stores the answer from the indexth position till the end, where mask is used to store the count of distinct primes included till now. Follow the steps below to solve the problem:
- Initialize a global multidimensional array dp[100][1<<4] with all values as -1 that stores the result of each recursive call.
- Index all the single digit prime numbers in ascending order using a HashMap.
- Define a recursive function, say countOfNumbers(index, mask, N) by performing the following steps.
- If the value of a index is equal to (N + 1),
- Count the number of distinct single digit primes included by finding the count of set bits in the mask.
- If the count is equal to 4, return 1 as a valid N-digit number has been formed.
- Otherwise return 0.
- If the result of the state dp[index][mask] is already computed, return this value dp[index][mask].
- If the current index is 1, then any digit from [1- 9] can be placed and if N = 1, then 0 can be placed as well.
- For all other index, any digit from [0-9] can be placed.
- If the current digit placed is a prime number, then set the index of the prime number to 1 in the bitmask.
- After making a valid placement, recursively call the countOfNumbers function for (index + 1).
- Return the sum of all possible valid placements of digits as the answer.
- If the value of a index is equal to (N + 1),
- Print the value returned by the function countOfNumbers(1, 0, N) as the result.
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-statesint dp[100][1 << 4]; // Stores the index of prime numbersmap<int, int> primeIndex; int countOfNumbers(int index, int mask, int N){ // If index = N+1 if (index == N + 1) { // Find count of distinct // prime numbers by counting // number of set bits. int countOfPrimes = __builtin_popcount(mask); // If count of distinct // prime numbers is equal to 4 // return 1. if (countOfPrimes == 4) { return 1; } return 0; } int& val = dp[index][mask]; // If the state has // already been computed if (val != -1) { return val; } val = 0; // If current position is 1, // then any digit from [1-9] can be placed. // If N = 1, 0 can be also placed. if (index == 1) { for (int digit = (N == 1 ? 0 : 1); digit <= 9; ++digit) { // If the digit is a prime number, // set the index of the // digit to 1 in the bitmask. if (primeIndex.find(digit) != primeIndex.end()) { val += countOfNumbers( index + 1, mask | (1 << primeIndex[digit]), N); } else { val += countOfNumbers(index + 1, mask, N); } } } // For remaining positions, // any digit from [0-9] can be placed else { for (int digit = 0; digit <= 9; ++digit) { // If the digit is a prime number, // set the index of the // digit to 1 in the bitmask. if (primeIndex.find(digit) != primeIndex.end()) { val += countOfNumbers( index + 1, mask | (1 << primeIndex[digit]), N); } else { val += countOfNumbers(index + 1, mask, N); } } } // Return the answer. return val;} // Driver Codeint main(){ // Initializing dp array with -1. memset(dp, -1, sizeof dp); // Indexing prime numbers in // ascending order primeIndex[2] = 0; primeIndex[3] = 1; primeIndex[5] = 2; primeIndex[7] = 3; // Given Input int N = 4; // Function call. cout << countOfNumbers(1, 0, N); return 0;} |
24
Time Complexity: O(N *10 * 24)
Auxiliary Space: O(N * 24)
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.


