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, 99Input: N = 3, K = 2
Output: 188
Naive Approach
The simplest approach is to iterate over all N digit numbers and check for every number if the adjacent digits have an absolute difference less than or equal to K.
Time Complexity: O(10N * N)
Efficient Approach:
To optimize the above approach, we need to use a Dynamic Programming approach along with Range Update
- Initialize a DP[][] array where dp[i][j] stores the count of numbers having i digits and ending with j.
- Iterate the array from 2 to N and check if the last digit was j, then the allowed digits for this place are in the range (max(0, j-k), min(9, j+k)). Perform a range update on this range.
- Now use Prefix Sum to get the actual answer.
Below is the implementation of the above approach:
C++
// C++ implementation of // the above approach #include <bits/stdc++.h> using namespace std; // Function to return count // of N-digit numbers with // absolute difference of // adjacent digits not // exceeding K long long getCount(int n, int k) { // For 1-digit numbers, // the count is 10 if (n == 1) return 10; long long dp[n + 1][11]; // dp[i][j] stores the number // of such i-digit numbers // ending in j for (int i = 0; i <= n; i++) { for (int j = 0; j < 11; j++) dp[i][j] = 0; } // Initialize count for // 1-digit numbers for (int i = 1; i <= 9; i++) dp[1][i] = 1; // Compute values for count of // digits greater than 1 for (int i = 2; i <= n; i++) { for (int j = 0; j <= 9; j++) { // Find the range of allowed // numbers if last digit is j int l = max(0, j - k); int r = min(9, j + k); // Perform Range update dp[i][l] += dp[i - 1][j]; dp[i][r + 1] -= dp[i - 1][j]; } // Prefix sum to find actual // values of i-digit numbers // ending in j for (int j = 1; j <= 9; j++) dp[i][j] += dp[i][j - 1]; } // Stores the final answer long long count = 0; for (int i = 0; i <= 9; i++) count += dp[n][i]; return count; } // Driver Code int main() { int N = 2, K = 1; cout << getCount(N, K); } |
Java
// Java Program to implement // the above approach import java.util.*; class GFG { // Function to return count of such numbers public static long getCount(int n, int k) { // For 1-digit numbers, the count // is 10 irrespective of K if (n == 1) return 10; // dp[i][j] stores the number // of such i-digit numbers // ending in j long dp[][] = new long[n + 1][11]; // Initialize count for // 1-digit numbers for (int i = 1; i <= 9; i++) dp[1][i] = 1; // Compute values for count of // digits greater than 1 for (int i = 2; i <= n; i++) { for (int j = 0; j <= 9; j++) { // Find the range of allowed // numbers if last digit is j int l = Math.max(0, j - k); int r = Math.min(9, j + k); // Perform Range update dp[i][l] += dp[i - 1][j]; dp[i][r + 1] -= dp[i - 1][j]; } // Prefix sum to find actual values // of i-digit numbers ending in j for (int j = 1; j <= 9; j++) dp[i][j] += dp[i][j - 1]; } // Stores the final answer long count = 0; for (int i = 0; i <= 9; i++) count += dp[n][i]; return count; } // Driver Code public static void main(String[] args) { int n = 2, k = 1; System.out.println(getCount(n, k)); } } |
Python3
# Python 3 Program to implement # the above approach # Function to return count # of N-digit numbers with # absolute difference of # adjacent digits not # exceeding K def getCount(n, k): # For 1-digit numbers, the # count is 10 if n == 1: return 10 # dp[i][j] stores the count of # i-digit numbers ending with j dp = [[0 for x in range(11)] for y in range(n + 1)]; # Initialize count for # 1-digit numbers for i in range(1, 10): dp[1][i]= 1 # Compute values for count # of digits greater than 1 for i in range(2, n + 1): for j in range(0, 10): # Find the range of allowed # numbers if last digit is j l = max(0, j - k) r = min(9, j + k) # Perform Range update dp[i][l] = dp[i][l] + dp[i-1][j] dp[i][r + 1] = dp[i][r + 1] - dp[i-1][j] # Prefix sum to find count of # of i-digit numbers ending with j for j in range(1, 10): dp[i][j] = dp[i][j] + dp[i][j-1] # Stores the final answer count = 0 for i in range(0, 10): count = count + dp[n][i] return count # Driver Code n, k = 2, 1print(getCount(n, k)) |
C#
// C# Program to implement // the above approach using System; class GFG { // Function to return the // count of N-digit numbers // with absolute difference of // adjacent digits not exceeding K static long getCount(int n, int k) { // For 1-digit numbers, the // count is 10 if (n == 1) return 10; // dp[i][j] stores the count of // i-digit numbers ending with j long[, ] dp = new long[n + 1, 11]; // Initialize count for // 1-digit numbers for (int i = 1; i <= 9; i++) dp[1, i] = 1; // Compute values for count of // digits greater than 1 for (int i = 2; i <= n; i++) { for (int j = 0; j <= 9; j++) { // Find the range of allowed // numbers with last digit j int l = Math.Max(0, j - k); int r = Math.Min(9, j + k); // Perform Range update dp[i, l] += dp[i - 1, j]; dp[i, r + 1] -= dp[i - 1, j]; } // Prefix sum to count i-digit // numbers ending in j for (int j = 1; j <= 9; j++) dp[i, j] += dp[i, j - 1]; } // Stores the final answer long count = 0; for (int i = 0; i <= 9; i++) count += dp[n, i]; return count; } // Driver Code public static void Main() { int n = 2, k = 1; Console.WriteLine(getCount(n, k)); } } |
26
Time Complexity: O(N)
Auxiliary Space: O(N)
Recommended Posts:
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K | Set 2
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Nth positive number whose absolute difference of adjacent digits is at most 1
- Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Arrange first N natural numbers such that absolute difference between all adjacent elements > 1
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Sort an Array based on the absolute difference of adjacent elements
- Minimize the maximum absolute difference of adjacent elements in a circular array
- Absolute difference between the first X and last X Digits of N
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Absolute difference between set and unset bit count in N
- Count pairs in an array such that the absolute difference between them is ≥ K
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


