Given an integer N, the task is to count the numbers up to N having an absolute difference of at most K between any two adjacent digits.
Note: Count of integer 0 for any digits is considerable.
Input: N = 20, K = 2
The required numbers are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 and 20. Notice that 14, 15, 16, 17, 18 and 19 all have adjacent digit’s absolute difference greater than K = 2 and thus they are not counted.
Input: N = 30, K = 3
All numbers upto 30 except 15, 16, 17, 18, 19, 26, 27, 28, 29 are accepted.
Naive Approach: The idea is to iterate till N and check for all the numbers that the difference of K exists or not. If yes then count it otherwise skip the number and keep iterating.
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: This problem can be optimized using Digit Dynamic Programming. Following are the detailed dp states for the given problem.
- In Digit Dp, we consider our number as a sequence of digits so a state position is needed so mark at which state we are currently at. In each recursive call, try to build the sequence from left to right by placing a digit from 0 to 9 and increment the position.
- Previous digit stores only those digits are put which have an absolute difference of atmost K from the previous digit. So, another state needed for the previous digit.
- Tight, state tells whether the number we are trying to build has already become smaller than N, so that in the upcoming recursive calls we can place any digit from 0 to 9. Otherwise, we can place till the digit of N at the current position.
- Initiliaze a boolean variable Start which tells whether the number has started or not. If the number has not yet started, we can start the number by placing digits from 1 to the upper limit with respect to tight in the current position. Otherwise, recur forward without starting the number.
- In each recursive call, set the current digit with respect to the previous digit such that the absolute difference between them never exceeds K. In the base case, return 1 if reached the last position.
Below is the implementation of the above approach:
Time Complexity: O( D * 10 * 2 * 2 * 10), considering N has D digits.
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 numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Generate all N digit numbers having absolute difference as K between adjacent digits
- Nth positive number whose absolute difference of adjacent digits is at most 1
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K | Set 2
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Remove Minimum coins such that absolute difference between any two piles is less than K
- Longest subarray in which absolute difference between any two element is not greater than X
- Count of Octal numbers upto N digits
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Count of numbers whose difference with Fibonacci count upto them is atleast K
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Count numbers formed by given two digit with sum having given digits
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Largest subset where absolute difference of any two element is a power of 2
- Count of subarrays of size K having at least one pair with absolute difference divisible by K-1
- Count of numbers upto M divisible by given Prime 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 email@example.com. 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.