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.
Input: N = 2, K = 1
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
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.
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:
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K
- Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Maximum possible sum of non-adjacent array elements not exceeding K
- Generate all N digit numbers having absolute difference as K between adjacent digits
- Count of all possible numbers not exceeding M having suffix N
- Nth positive number whose absolute difference of adjacent digits is at most 1
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Count number of triangles possible with length of sides not exceeding N
- Maximize count of pairs (i, j) from two arrays having element from first array not exceeding that from second array
- Count array elements with rank not exceeding K
- Arrange first N natural numbers such that absolute difference between all adjacent elements > 1
- Kth number exceeding N whose sum of its digits divisible by M
- Maximum students to pass after giving bonus to everybody and not exceeding 100 marks
- Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Count array elements exceeding sum of preceding K elements
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.