Count of N-digit numbers with absolute difference of adjacent digits not exceeding K | Set 2
Given two integers N and K, the task is to find the count of N-digit numbers such that the absolute difference of adjacent digits in the number is not greater than K.
Examples:
Input: N = 2, K = 1
Output: 26
Explanation: The numbers are 10, 11, 12, 21, 22, 23, 32, 33, 34, 43, 44, 45, 54, 55, 56, 65, 66, 67, 76, 77, 78, 87, 88, 89, 98, 99
Input: N = 3, K = 2
Output: 188
Naive Approach and Dynamic Programming Approach: Refer to Count of N-digit numbers with absolute difference of adjacent digits not exceeding K for the simplest solution and the dynamic programming approach that requires O(N) auxiliary space.
Space-Efficient Approach:
Follow the steps below to optimize the above approach:
- In the above approach, a 2D array dp[][] is initialized where dp[i][j] stores the count of numbers having i digits and ending with j.
- It can be observed that the answer for any length i depends only on the count generated for i – 1. So, instead of a 2D array, initialize an array dp[] of size of all possible digits, and for every i upto N, dp[j] stores count of such numbers of length i ending with digit j.
- Initialize another array next[] of size of all possible digits.
- Since the count of single-digit numbers ending with a value j is always 1, fill dp[] with 1 at all indices initially.
- Iterate over the range [2, N], and for every value in the range i, check if the last digit is j, then the allowed digits for this place are in the range (max(0, j-k), min(9, j+k)). Perform a range update:
next[l] = next[l] + dp[j]
next[r + 1] = next[r + 1] – dp[j]
where l and r are max(0, j – k) and min(9, j + k) respectively.
- Once the above step is completed for a particular value of i, compute prefix sum of next[] and update dp[] with the values of next[].
Below is the implementation of the above approach:
C
#include <stdio.h>
int max( int num1, int num2)
{
return (num1 > num2 ) ? num1 : num2;
}
int min( int num1, int num2)
{
return (num1 > num2 ) ? num2 : num1;
}
int getCount( int n, int k)
{
if (n == 1)
return 10;
int dp[11] = {0};
int next[11] = {0};
for ( int i = 1; i <= 9; i++)
dp[i] = 1;
for ( int i = 2; i <= n; i++)
{
for ( int j = 0; j <= 9; j++)
{
int l = max(0, (j - k));
int r = min(9, (j + k));
next[l] += dp[j];
next[r + 1] -= dp[j];
}
for ( int j = 1; j <= 9; j++)
next[j] += next[j - 1];
for ( int j = 0; j < 10; j++)
{
dp[j] = next[j];
next[j] = 0;
}
}
int count = 0;
for ( int i = 0; i <= 9; i++)
count += dp[i];
return count;
}
int main()
{
int n = 2, k = 1;
printf ( "%d" , getCount(n, k));
}
|
C++
#include <bits/stdc++.h>
using namespace std;
int max( int num1, int num2)
{
return (num1 > num2 ) ? num1 : num2;
}
int min( int num1, int num2)
{
return (num1 > num2 ) ? num2 : num1;
}
int getCount( int n, int k)
{
if (n == 1)
return 10;
int dp[11] = {0};
int next[11] = {0};
for ( int i = 1; i <= 9; i++)
dp[i] = 1;
for ( int i = 2; i <= n; i++)
{
for ( int j = 0; j <= 9; j++)
{
int l = max(0, (j - k));
int r = min(9, (j + k));
next[l] += dp[j];
next[r + 1] -= dp[j];
}
for ( int j = 1; j <= 9; j++)
next[j] += next[j - 1];
for ( int j = 0; j < 10; j++)
{
dp[j] = next[j];
next[j] = 0;
}
}
int count = 0;
for ( int i = 0; i <= 9; i++)
count += dp[i];
return count;
}
int main()
{
int n = 2, k = 1;
cout << getCount(n, k);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static long getCount(
int n, int k)
{
if (n == 1 )
return 10 ;
long dp[] = new long [ 11 ];
long next[] = new long [ 11 ];
for ( int i = 1 ; i <= 9 ; i++)
dp[i] = 1 ;
for ( int i = 2 ; i <= n; i++) {
for ( int j = 0 ; j <= 9 ; j++) {
int l = Math.max( 0 , j - k);
int r = Math.min( 9 , j + k);
next[l] += dp[j];
next[r + 1 ] -= dp[j];
}
for ( int j = 1 ; j <= 9 ; j++)
next[j] += next[j - 1 ];
for ( int j = 0 ; j < 10 ; j++) {
dp[j] = next[j];
next[j] = 0 ;
}
}
long count = 0 ;
for ( int i = 0 ; i <= 9 ; i++)
count += dp[i];
return count;
}
public static void main(String[] args)
{
int n = 2 , k = 1 ;
System.out.println(getCount(n, k));
}
}
|
Python3
def getCount(n, K):
if (n = = 1 ):
return 10
dp = [ 0 ] * 11
next = [ 0 ] * 11
for i in range ( 1 , 9 + 1 ):
dp[i] = 1
for i in range ( 2 , n + 1 ):
for j in range ( 9 + 1 ):
l = max ( 0 , j - k)
r = min ( 9 , j + k)
next [l] + = dp[j]
next [r + 1 ] - = dp[j]
for j in range ( 1 , 9 + 1 ):
next [j] + = next [j - 1 ]
for j in range ( 10 ):
dp[j] = next [j]
next [j] = 0
count = 0
for i in range ( 9 + 1 ):
count + = dp[i]
return count
if __name__ = = '__main__' :
n = 2
k = 1
print (getCount(n, k))
|
C#
using System;
class GFG{
public static long getCount( int n, int k)
{
if (n == 1)
return 10;
long []dp = new long [11];
long []next = new long [11];
for ( int i = 1; i <= 9; i++)
dp[i] = 1;
for ( int i = 2; i <= n; i++)
{
for ( int j = 0; j <= 9; j++)
{
int l = Math.Max(0, j - k);
int r = Math.Min(9, j + k);
next[l] += dp[j];
next[r + 1] -= dp[j];
}
for ( int j = 1; j <= 9; j++)
next[j] += next[j - 1];
for ( int j = 0; j < 10; j++)
{
dp[j] = next[j];
next[j] = 0;
}
}
long count = 0;
for ( int i = 0; i <= 9; i++)
count += dp[i];
return count;
}
public static void Main(String[] args)
{
int n = 2, k = 1;
Console.WriteLine(getCount(n, k));
}
}
|
Javascript
<script>
function max(num1, num2)
{
return (num1 > num2 ) ? num1 : num2;
}
function min(num1, num2)
{
return (num1 > num2 ) ? num2 : num1;
}
function getCount(n, k)
{
if (n == 1)
return 10;
var dp = Array(11).fill(0);
var next = Array(11).fill(0);
for ( var i = 1; i <= 9; i++)
dp[i] = 1;
for ( var i = 2; i <= n; i++)
{
for ( var j = 0; j <= 9; j++)
{
var l = Math.max(0, (j - k));
var r = Math.min(9, (j + k));
next[l] += dp[j];
next[r + 1] -= dp[j];
}
for ( var j = 1; j <= 9; j++)
next[j] += next[j - 1];
for ( var j = 0; j < 10; j++)
{
dp[j] = next[j];
next[j] = 0;
}
}
var count = 0;
for ( var i = 0; i <= 9; i++)
count += dp[i];
return count;
}
var n = 2, k = 1;
document.write( getCount(n, k));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
18 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...