
Consider L = 10, R = 13.
Then, all integers in range [10, 13] are 10, 11, 12, 13.
We can say, that 10 is a stepping number because |1 - 0| = 1.
11 is not a stepping number because |1 - 1| = 0.
12 is a stepping number because |1 - 2| = 1.
13 is not a stepping number because |1 - 3| = 2.
Thus, we should return a list [10, 12].
The first line of input contains an integer ‘T’ denoting the number of test cases. Then, ‘T’ test cases follow.
The first and the only line of each test case consist of two space-separated integers ‘L’ and ‘R’ respectively.
For each test case, if there are ‘K’ stepping numbers between ‘L’ and ‘R’, then print a single line consisting of ‘K’ space-separated integers representing Stepping Numbers between ‘L’ and ‘R’ in increasing order.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= L <= 10^8
1 <= R <= 10^8
R >= L
Time Limit: 1 second
This approach is straightforward, we iterate over all integers from ‘L’ to ‘R’ and for each integer, we check whether it is a stepping number or not. If the integer is a stepping number then we will append it in the resultant list. We can check whether a number is a stepping number or not by simply iterating over its digit as described in the algorithm mentioned below.
Consider a directed graph where every node represents a stepping number. There will be a directed edge from node ‘u’ to ‘v’, if the stepping number ‘u’ can be converted into stepping number ‘v’ by adding one more digit. i.e there will be directed edge from ‘u’ to ‘v if either:
or v = u*10 + u % 10 + 1 (u % 10!= 9)
After this, we can solve this problem using Depth-First Search (DFS) or Breadth-First Search (BFS) algorithm. We have described a DFS based algorithm below, The problem can be solved similarly using BFS similarly.