Given a positive integer N, the task is to count the number of N-digit numbers having absolute difference between consecutive digits in non-increasing order.
Examples:
Input: N = 1
Output: 10
Explanation:
All numbers from 0 to 9 satisfy the given condition as there is only one digit.
Input: N = 3
Output: 495
Naive Approach: The simplest approach to solve the given problem is to iterate over all possible N-digit numbers and count those numbers whose digits are in non-increasing order. After checking for all the numbers, print the value of count as the result.
Time Complexity: O(N * 10N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by using Dynamic Programming because the above problem 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 digitth position till the end, when the previous digit selected, is prev1 and the second previous digit selected is prev2. Follow the steps below to solve the problem:
- Define a recursive function, say countOfNumbers(digit, prev1, prev2) by performing the following steps.
- 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.
- Otherwise iterate through all the numbers from i = 0 to i = 9, and check if the condition (abs(prev1 – i) <= abs(prev1 – prev2) ) holds valid or not and accordingly place satisfying ‘i’ values in 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.
- Print the value returned by the function countOfNumbers(1, 0, 0, N) as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int dp[100][10][10];
int countOfNumbers( int digit, int prev1,
int prev2, int n)
{
if (digit == n + 1) {
return 1;
}
int & val = dp[digit][prev1][prev2];
if (val != -1) {
return val;
}
val = 0;
if (digit == 1) {
for ( int i = (n == 1 ? 0 : 1);
i <= 9; ++i) {
val += countOfNumbers(
digit + 1, i, prev1, n);
}
}
else if (digit == 2) {
for ( int i = 0; i <= 9; ++i) {
val += countOfNumbers(
digit + 1, i, prev1, n);
}
}
else {
int diff = abs (prev2 - prev1);
for ( int i = 0; i <= 9; ++i) {
if ( abs (prev1 - i) <= diff) {
val += countOfNumbers(
digit + 1, i,
prev1, n);
}
}
}
return val;
}
int countNumbersUtil( int N)
{
memset (dp, -1, sizeof dp);
cout << countOfNumbers(1, 0, 0, N);
}
int main()
{
int N = 3;
countNumbersUtil(N);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int dp[][][] = new int [ 100 ][ 10 ][ 10 ];
static int countOfNumbers( int digit, int prev1,
int prev2, int n)
{
if (digit == n + 1 )
{
return 1 ;
}
int val = dp[digit][prev1][prev2];
if (val != - 1 )
{
return val;
}
val = 0 ;
if (digit == 1 )
{
for ( int i = (n == 1 ? 0 : 1 );
i <= 9 ; ++i)
{
val += countOfNumbers(
digit + 1 , i, prev1, n);
}
}
else if (digit == 2 )
{
for ( int i = 0 ; i <= 9 ; ++i)
{
val += countOfNumbers(
digit + 1 , i, prev1, n);
}
}
else
{
int diff = Math.abs(prev2 - prev1);
for ( int i = 0 ; i <= 9 ; ++i)
{
if (Math.abs(prev1 - i) <= diff)
{
val += countOfNumbers(
digit + 1 , i,
prev1, n);
}
}
}
return val;
}
static void countNumbersUtil( int N)
{
for ( int i = 0 ; i < 100 ; i++)
{
for ( int j = 0 ; j < 10 ; j++)
{
for ( int k = 0 ; k < 10 ; k++)
{
dp[i][j][k] = - 1 ;
}
}
}
System.out.println(countOfNumbers( 1 , 0 , 0 , N));
}
public static void main(String[] args)
{
int N = 3 ;
countNumbersUtil(N);
}
}
|
Python3
dp = [[[ 0 for i in range ( 10 )]
for col in range ( 10 )]
for row in range ( 100 )]
def countOfNumbers(digit, prev1, prev2, n):
if (digit = = n + 1 ):
return 1
val = dp[digit][prev1][prev2]
if (val ! = - 1 ):
return val
val = 0
if (digit = = 1 ):
i = 1
if n = = 1 :
i = 0
for j in range (i, 10 ):
val + = countOfNumbers(digit + 1 , j, prev1, n)
elif (digit = = 2 ):
for i in range ( 0 , 10 ):
val + = countOfNumbers(digit + 1 , i, prev1, n)
else :
diff = abs (prev2 - prev1)
for i in range ( 0 , 10 ):
if ( abs (prev1 - i) < = diff):
val + = countOfNumbers(digit + 1 , i, prev1, n)
return val
def countNumbersUtil(N):
for i in range ( 0 , 100 ):
for j in range ( 0 , 10 ):
for k in range ( 0 , 10 ):
dp[i][j][k] = - 1
print (countOfNumbers( 1 , 0 , 0 , N))
N = 3
countNumbersUtil(N)
|
C#
using System;
class GFG{
static int [,,] dp = new int [100, 10, 10];
static int countOfNumbers( int digit, int prev1,
int prev2, int n)
{
if (digit == n + 1)
{
return 1;
}
int val = dp[digit, prev1, prev2];
if (val != -1)
{
return val;
}
val = 0;
if (digit == 1)
{
for ( int i = (n == 1 ? 0 : 1);
i <= 9; ++i)
{
val += countOfNumbers(
digit + 1, i, prev1, n);
}
}
else if (digit == 2)
{
for ( int i = 0; i <= 9; ++i)
{
val += countOfNumbers(
digit + 1, i, prev1, n);
}
}
else
{
int diff = Math.Abs(prev2 - prev1);
for ( int i = 0; i <= 9; ++i)
{
if (Math.Abs(prev1 - i) <= diff)
{
val += countOfNumbers(
digit + 1, i,
prev1, n);
}
}
}
return val;
}
static void countNumbersUtil( int N)
{
for ( int i = 0; i < 100; i++)
{
for ( int j = 0; j < 10; j++)
{
for ( int k = 0; k < 10; k++)
{
dp[i, j, k] = -1;
}
}
}
Console.WriteLine(countOfNumbers(1, 0, 0, N));
}
static public void Main()
{
int N = 3;
countNumbersUtil(N);
}
}
|
Javascript
<script>
var dp = Array(100).fill().map(() => Array(10).fill(0).map(()=>Array(10).fill(0)));
function countOfNumbers(digit , prev1 , prev2 , n) {
if (digit == n + 1) {
return 1;
}
var val = dp[digit][prev1][prev2];
if (val != -1) {
return val;
}
val = 0;
if (digit == 1) {
for ( var i = (n == 1 ? 0 : 1); i <= 9; ++i) {
val += countOfNumbers(digit + 1, i, prev1, n);
}
}
else if (digit == 2) {
for ( var i = 0; i <= 9; ++i) {
val += countOfNumbers(digit + 1, i, prev1, n);
}
}
else {
var diff = Math.abs(prev2 - prev1);
for ( var i = 0; i <= 9; ++i) {
if (Math.abs(prev1 - i) <= diff) {
val += countOfNumbers(digit + 1, i, prev1, n);
}
}
}
return val;
}
function countNumbersUtil(N) {
for ( var i = 0; i < 100; i++) {
for ( var j = 0; j < 10; j++) {
for ( var k = 0; k < 10; k++) {
dp[i][j][k] = -1;
}
}
}
document.write(countOfNumbers(1, 0, 0, N));
}
var N = 3;
countNumbersUtil(N);
</script>
|
Time Complexity: O(N * 103)
Auxiliary Space: O(N * 102)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
16 Nov, 2021
Like Article
Save Article