The Wayback Machine - https://web.archive.org/web/20210602172821/https://www.geeksforgeeks.org/count-n-digit-numbers-whose-digits-does-not-exceed-absolute-difference-of-the-two-previous-digits/
Skip to content
Related Articles

Related Articles

Count N-digit numbers whose digits does not exceed absolute difference of the two previous digits
  • Last Updated : 02 Jun, 2021

Given an integer N, the task is to count the number of N-digit numbers such that each digit, except the first and second digits, is less than or equal to the absolute difference of the previous two digits.

Examples:

Input: N = 1
Output: 10
Explanation: All the numbers from [0 – 9] are valid because the number of digits is 1.

Input : N = 3
Output : 375

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

Efficient Approach: In the efficient approach, all possible numbers are constructed instead of verifying the conditions on a range of numbers. This can be achieved with the help of Dynamic Programming because it has overlapping subproblems and optimal substructure. The subproblems can be stored in dp[][][] table using memoization where dp[digit][prev1][prev2] stores the answer from the digit-th position till the end when the previous digit selected is prev1 and the second most previous digit selected is prev2.



Follow the below steps to solve the problem:

  • Define a recursive function countOfNumbers(digit, prev1, prev2) by performing the following steps.
    • Check the base cases. If the value of digit is equal to N+1 then return 1 as a valid N-digit number is formed.
    • If the result of the state dp[digit][prev1][prev2] is already computed, return this state dp[digit][prev1][prev2].
    • If the current digit is 1, then any digit from [1-9] can be placed. If N=1, then 0 can be placed as well.
    • If the current digit is 2, then any digit from [0-9] can be placed.
    • Else any number from [0-(abs(prev1-prev2))] can be placed at the current position.
    • 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 code for the above approach:

C++




// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
long long dp[50][10][10];
  
// Function to count N digit numbers whose
// digits are less than or equal to the
// absolute difference of previous two digits
long long countOfNumbers(int digit, int prev1,
                         int prev2, int N)
{
    // If all digits are traversed
    if (digit == N + 1)
        return 1;
  
    // If the state has already been computed
    if (dp[digit][prev1][prev2] != -1)
        return dp[digit][prev1][prev2];
  
    dp[digit][prev1][prev2] = 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 j = (N == 1 ? 0 : 1);
             j <= 9; ++j) {
  
            dp[digit][prev1][prev2]
                += countOfNumbers(digit + 1,
                                  j, prev1, N);
        }
    }
  
    // If the current digit is 2, any
    // digit from [0-9] can be placed
    else if (digit == 2) {
        for (int j = 0; j <= 9; ++j) {
  
            dp[digit][prev1][prev2]
                += countOfNumbers(
                    digit + 1, j, prev1, N);
        }
    }
  
    // For other digits, any digit
    // from 0 to abs(prev1 - prev2) can be placed
    else {
        for (int j = 0; j <= abs(prev1 - prev2); ++j) {
  
            dp[digit][prev1][prev2]
                += countOfNumbers(digit + 1, j, prev1, N);
        }
    }
  
    // Return the answer
    return dp[digit][prev1][prev2];
}
  
// Driver code
int main()
{
    // Initializing dp array with -1.
    memset(dp, -1, sizeof dp);
  
    // Input
    int N = 3;
  
    // Function call
    cout << countOfNumbers(1, 0, 0, N) << endl;
  
    return 0;
}
Output:
375

Time Complexity : O(N * 103)
Auxiliary Space: O(N * 102) 

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 :