Given two integers ‘n’ and ‘m’, find all the stepping numbers in range [n, m]. A number is called stepping number if all adjacent digits have an absolute difference of 1. 321 is a Stepping Number while 421 is not.
Input : n = 0, m = 21 Output : 0 1 2 3 4 5 6 7 8 9 10 12 21 Input : n = 10, m = 15 Output : 10, 12
Method 1 : Brute force Approach
In this method, a brute force approach is used to iterate through all the integers from n to m and check if it’s a stepping number.
0 1 2 3 4 5 6 7 8 9 10 12 21
Method 2: Using BFS/DFS
How to build the graph?
Every node in the graph represents a stepping number; there will be a directed edge from a node U to V if V can be transformed from U. (U and V are Stepping Numbers) A Stepping Number V can be transformed from U in following manner.
lastDigit refers to the last digit of U (i.e. U % 10)
An adjacent number V can be:
- U*10 + lastDigit + 1 (Neighbor A)
- U*10 + lastDigit – 1 (Neighbor B)
By applying above operations a new digit is appended to U, it is either lastDigit-1 or lastDigit+1, so that the new number V formed from U is also a Stepping Number.
Therefore, every Node will have at most 2 neighboring Nodes.
Edge Cases: When the last digit of U is 0 or 9
Case 1: lastDigit is 0 : In this case only digit ‘1’ can be appended.
Case 2: lastDigit is 9 : In this case only digit ‘8’ can be appended.
What will be the source/starting Node?
- Every single digit number is considered as a stepping Number, so bfs traversal for every digit will give all the stepping numbers starting from that digit.
- Do a bfs/dfs traversal for all the numbers from [0,9].
Note: For node 0, no need to explore neighbors during BFS traversal since it will lead to 01, 012, 010 and these will be covered by the BFS traversal starting from node 1.
Example to find all the stepping numbers from 0 to 21
-> 0 is a stepping Number and it is in the range so display it. -> 1 is a Stepping Number, find neighbors of 1 i.e., 10 and 12 and push them into the queue How to get 10 and 12? Here U is 1 and last Digit is also 1 V = 10 + 0 = 10 ( Adding lastDigit - 1 ) V = 10 + 2 = 12 ( Adding lastDigit + 1 ) Then do the same for 10 and 12 this will result into 101, 123, 121 but these Numbers are out of range. Now any number transformed from 10 and 12 will result into a number greater than 21 so no need to explore their neighbors. -> 2 is a Stepping Number, find neighbors of 2 i.e. 21, 23. -> 23 is out of range so it is not considered as a Stepping Number (Or a neighbor of 2) The other stepping numbers will be 3, 4, 5, 6, 7, 8, 9.
BFS based Solution:
DFS based Solution
0 1 10 12 2 21 3 4 5 6 7 8 9
This article is contributed by Chirag Agarwal. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Sum of two large numbers
- GCD of more than two (or array) numbers
- Count numbers with same first and last digits
- Generate all palindromic numbers less than n
- Program for Fibonacci numbers
- Find two prime numbers with given sum
- Find the first N pure numbers
- Find top k (or most frequent) numbers in a stream
- Calculate sum of all numbers present in a string
- Add two numbers represented by linked lists | Set 1
- Count total set bits in all numbers from 1 to n
- Add two numbers represented by linked lists | Set 2
- Find subarray with given sum | Set 1 (Nonnegative Numbers)
- Numbers having Unique (or Distinct) digits
- Find k numbers with most occurrences in the given array
- Find k closest numbers in an unsorted array
- Count of numbers in the range [L, R] which satisfy the given conditions
- Prove that atleast one of three consecutive even numbers is divisible by 6
- Print all Jumping Numbers smaller than or equal to a given value
Improved By : nitin mittal