Count of N-digit numbers in base K with no two consecutive zeroes - GeeksforGeeks

13 Mar, 2022
Given two integers N and K, the task is to find the count of all the integer in base K which satisfy the following conditions:  
The integers must be of exactly N digits.There should be no leading 0.There must not be any consecutive digit pair such that both the digits are 0.
The integers must be of exactly N digits.
There should be no leading 0.
There must not be any consecutive digit pair such that both the digits are 0.
Examples:  
Input: N = 3, K = 10 Output: 891 All 3-digit numbers in base 10 are from the range [100, 999] Out of these numbers only 100, 200, 300, 400, ..., 900 are invalid. Hence valid integers are 900 – 9 = 891Input: N = 2, K = 10 Output: 90  
 
Naive approach: Write a function count_numbers(int k, int n, bool flag) which will return the count of N digit numbers possible of base K and flag will tell whether the previously chosen digit was 0 or not. Call this function recursively for count_numbers(int k, int n-1, bool flag), and using the flag, the occurrence of two consecutive 0s can be avoided.Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the count of// n-digit numbers that satisfy// the given conditionsint count_numbers(int k, int n, bool flag){     // Base case    if (n == 1) {         // If 0 wasn't chosen previously        if (flag) {            return (k - 1);        }        else {            return 1;        }    }     // If 0 wasn't chosen previously    if (flag)        return (k - 1) * (count_numbers(k, n - 1, 0)                          + count_numbers(k, n - 1, 1));    else        return count_numbers(k, n - 1, 1);} // Driver codeint main(){    int n = 3;    int k = 10;    cout << count_numbers(k, n, true);     return 0;}
// Java implementation of the approachclass GFG{     // Function to return the count of// n-digit numbers that satisfy// the given conditionsstatic int count_numbers(int k, int n,                         boolean flag){     // Base case    if (n == 1)    {         // If 0 wasn't chosen previously        if (flag)        {            return (k - 1);        }        else        {            return 1;        }    }     // If 0 wasn't chosen previously    if (flag)        return (k - 1) * (count_numbers(k, n - 1, false) +                          count_numbers(k, n - 1, true));    else        return count_numbers(k, n - 1, true);} // Driver codepublic static void main (String[] args){    int n = 3;    int k = 10;    System.out.println(count_numbers(k, n, true));}} // This code is contributed by AnkitRai01
# Python3 implementation of the approach # Function to return the count of# n-digit numbers that satisfy# the given conditionsdef count_numbers(k, n, flag) :     # Base case    if (n == 1) :         # If 0 wasn't chosen previously        if (flag) :            return (k - 1)        else :            return 1     # If 0 wasn't chosen previously    if (flag) :        return (k - 1) * (count_numbers(k, n - 1, 0) +                          count_numbers(k, n - 1, 1))    else :        return count_numbers(k, n - 1, 1) # Driver coden = 3k = 10print(count_numbers(k, n, True)) # This code is contributed by# divyamohan123
// C# implementation of the approachusing System;                     class GFG{     // Function to return the count of// n-digit numbers that satisfy// the given conditionsstatic int count_numbers(int k, int n,                         bool flag){     // Base case    if (n == 1)    {         // If 0 wasn't chosen previously        if (flag)        {            return (k - 1);        }        else        {            return 1;        }    }     // If 0 wasn't chosen previously    if (flag)        return (k - 1) * (count_numbers(k, n - 1, false) +                          count_numbers(k, n - 1, true));    else        return count_numbers(k, n - 1, true);} // Driver codepublic static void Main (String[] args){    int n = 3;    int k = 10;    Console.Write(count_numbers(k, n, true));}} // This code is contributed by 29AjayKuma
<script>// Javascript implementation of the approach // Function to return the count of// n-digit numbers that satisfy// the given conditionsfunction count_numbers(k, n, flag){     // Base case    if (n == 1) {         // If 0 wasn't chosen previously        if (flag) {            return (k - 1);        }        else {            return 1;        }    }     // If 0 wasn't chosen previously    if (flag)        return (k - 1) * (count_numbers(k, n - 1, 0)                          + count_numbers(k, n - 1, 1));    else        return count_numbers(k, n - 1, 1);} // Driver code    let n = 3;    let k = 10;    document.write(count_numbers(k, n, true)); // This code is contributed by souravmahato348.</script>
891
 
Time Complexity: O(n2)
Auxiliary Space: O(n2)
Efficient approach: Now that the problem has been solved using recursion, a 2-D DP can be used to solve this problem dp[n + 1][2] where dp[i][0] will give the number of i digit numbers possible ending with 0 and dp[i][1] will give the number of i digit numbers possible ending with non-zero. The recurrence relation will be:  
dp[i][0] = dp[i – 1][1]; dp[i][1] = (dp[i – 1][0] + dp[i – 1][1]) * (K – 1);  
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the count of// n-digit numbers that satisfy// the given conditionsint count_numbers(int k, int n){     // DP array to store the    // pre-calculated states    int dp[n + 1][2];     // Base cases    dp[1][0] = 0;    dp[1][1] = k - 1;    for (int i = 2; i <= n; i++) {         // i-digit numbers ending with 0        // can be formed by concatenating        // 0 in the end of all the (i - 1)-digit        // number ending at a non-zero digit        dp[i][0] = dp[i - 1][1];         // i-digit numbers ending with non-zero        // can be formed by concatenating any non-zero        // digit in the end of all the (i - 1)-digit        // number ending with any digit        dp[i][1] = (dp[i - 1][0] + dp[i - 1][1]) * (k - 1);    }     // n-digit number ending with    // and ending with non-zero    return dp[n][0] + dp[n][1];} // Driver codeint main(){    int k = 10;    int n = 3;    cout << count_numbers(k, n);     return 0;}
// Java implementation of the approachimport java.util.*; class GFG{ // Function to return the count of// n-digit numbers that satisfy// the given conditionsstatic int count_numbers(int k, int n){     // DP array to store the    // pre-calculated states    int [][]dp = new int[n + 1][2];     // Base cases    dp[1][0] = 0;    dp[1][1] = k - 1;    for (int i = 2; i <= n; i++)    {         // i-digit numbers ending with 0        // can be formed by concatenating        // 0 in the end of all the (i - 1)-digit        // number ending at a non-zero digit        dp[i][0] = dp[i - 1][1];         // i-digit numbers ending with non-zero        // can be formed by concatenating any non-zero        // digit in the end of all the (i - 1)-digit        // number ending with any digit        dp[i][1] = (dp[i - 1][0] +                    dp[i - 1][1]) * (k - 1);    }     // n-digit number ending with    // and ending with non-zero    return dp[n][0] + dp[n][1];} // Driver codepublic static void main(String[] args){    int k = 10;    int n = 3;    System.out.println(count_numbers(k, n));}} // This code is contributed by Rajput-Ji
# Python3 implementation of the approach # Function to return the count of# n-digit numbers that satisfy# the given conditionsdef count_numbers(k, n):     # DP array to store the    # pre-calculated states    dp = [[0 for i in range(2)]             for i in range(n + 1)]     # Base cases    dp[1][0] = 0    dp[1][1] = k - 1    for i in range(2, n + 1):         # i-digit numbers ending with 0        # can be formed by concatenating        # 0 in the end of all the (i - 1)-digit        # number ending at a non-zero digit        dp[i][0] = dp[i - 1][1]         # i-digit numbers ending with non-zero        # can be formed by concatenating any non-zero        # digit in the end of all the (i - 1)-digit        # number ending with any digit        dp[i][1] = (dp[i - 1][0] +                    dp[i - 1][1]) * (k - 1)     # n-digit number ending with    # and ending with non-zero    return dp[n][0] + dp[n][1] # Driver codek = 10n = 3print(count_numbers(k, n)) # This code is contributed by Mohit Kumar
// C# implementation of the approachusing System;                     class GFG{ // Function to return the count of// n-digit numbers that satisfy// the given conditionsstatic int count_numbers(int k, int n){     // DP array to store the    // pre-calculated states    int [,]dp = new int[n + 1, 2];     // Base cases    dp[1, 0] = 0;    dp[1, 1] = k - 1;    for (int i = 2; i <= n; i++)    {         // i-digit numbers ending with 0        // can be formed by concatenating        // 0 in the end of all the (i - 1)-digit        // number ending at a non-zero digit        dp[i, 0] = dp[i - 1, 1];         // i-digit numbers ending with non-zero        // can be formed by concatenating any non-zero        // digit in the end of all the (i - 1)-digit        // number ending with any digit        dp[i, 1] = (dp[i - 1, 0] +                    dp[i - 1, 1]) * (k - 1);    }     // n-digit number ending with    // and ending with non-zero    return dp[n, 0] + dp[n, 1];} // Driver codepublic static void Main(String[] args){    int k = 10;    int n = 3;    Console.WriteLine(count_numbers(k, n));}} // This code is contributed by 29AjayKumar
<script> // Javascript implementation of the approach // Function to return the count of// n-digit numbers that satisfy// the given conditionsfunction count_numbers(k, n){     // DP array to store the    // pre-calculated states    let dp = new Array(n + 1);    for (let i = 0; i < n + 1; i++)        dp[i] = new Array(2);     // Base cases    dp[1][0] = 0;    dp[1][1] = k - 1;    for (let i = 2; i <= n; i++) {         // i-digit numbers ending with 0        // can be formed by concatenating        // 0 in the end of all the (i - 1)-digit        // number ending at a non-zero digit        dp[i][0] = dp[i - 1][1];         // i-digit numbers ending with non-zero        // can be formed by concatenating any non-zero        // digit in the end of all the (i - 1)-digit        // number ending with any digit        dp[i][1] = (dp[i - 1][0] +                    dp[i - 1][1]) * (k - 1);    }     // n-digit number ending with    // and ending with non-zero    return dp[n][0] + dp[n][1];} // Driver code    let k = 10;    let n = 3;    document.write(count_numbers(k, n)); </script>
891
 
divyamohan123
ankthon
mohit kumar 29
29AjayKumar
Rajput-Ji
souravmahato348
subhammahato348
saurabh1990aror
digit-DP
Dynamic Programming
Mathematical
Dynamic Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Bellman–Ford Algorithm | DP-23
Floyd Warshall Algorithm | DP-16
Subset Sum Problem | DP-25
Coin Change | DP-7
Matrix Chain Multiplication | DP-8
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7
Merge two sorted arrays