Stepping Numbers

Moderate
0/80
Average time to solve is 30m
profile
Contributed by
4 upvotes
Asked in company
Amazon

Problem statement

You are given two positive integers ‘L’ and ‘R’. Find all the Stepping Numbers in the range [L, R].

A positive integer is called a Stepping Number if the adjacent digits have a difference of 1. For example, 121 is a Stepping Number because |1-2| = 1 and |2 -1| = 1. All single-digit integers will also be considered as Stepping Numbers.

You should return a list of Stepping Numbers between ‘L’ and ‘R’ (inclusive) in the increasing order.

For Example:
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].
Detailed explanation ( Input/output format, Notes, Images )
Input format:
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.
Output format :
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.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 50
1 <= L <= 10^8
1 <= R <= 10^8
R >= L

Time Limit: 1 second
Sample Input 1:
2
1 5
10 13
Sample Output 1:
1 2 3 4 5
10 12
Explanation For Sample Output 1:
Test case 1:
Single-digit integers are stepping numbers, Thus all integers in the range [1, 5] will be stepping numbers.

Test case 2:
See the problem statement for an explanation.
Sample Input 2:
2
23 50
22 23
Sample Output 2:
23 32 34 43 45
23
Hint

One by one check for all integers between ‘L’ and ‘R’, whether they are stepping numbers or not.

Approaches (2)
Brute Force Approach

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.

 

Algorithm

 

  • Create an empty list of integers ‘result’.
  • Run a loop where ‘i’ ranges from ‘L’ to ‘R’ and for each ‘i’ do the following:
    • Initialize an integer variable curNum := i.
    • If curNum < 10, then append curNum in the result and skip the current iteration.
    • Initialize an integer variable nextDigit:= curNum % 10 and then do curNum:= curNum / 10.
    • Initialize a boolean variable flag:= true.
    • Run a while loop till curNum > 0 and in each iteration do the following
      • If abs(curNum % 10 - nextDigit) != 1, then do flag:= false and break the loop
      • Otherwise, do nextDigit:= curNum % 10 and curNum: = curNum /= 10.
  • If flag = true, then append ‘i’ in ‘result’.
  • Return result.
Time Complexity

O((R - L) * log(R)), where ‘R’ and ‘L’ are the given integers.

 

The number of digits in any integer ‘X’ is of the order of log(X) (taking log base 10). Thus it will take log(X) to test whether an integer ‘X’ is a stepping number or not. Since there are (R-L) integers that we need to test, and the largest number can be ‘R’,

 

 Thus the overall complexity will be O((R - L) * log(R)).  

Space Complexity

O(1).

 

We are not using any extra space here.

Code Solution
(100% EXP penalty)
Stepping Numbers
Full screen
Console