A number is called as a Jumping Number if all adjacent digits in it differ by 1. The difference between ‘9’ and ‘0’ is not considered as 1.
All single digit numbers are considered as Jumping Numbers. For example 7, 8987 and 4343456 are Jumping numbers but 796 and 89098 are not.
Given a positive number x, print all Jumping Numbers smaller than or equal to x. The numbers can be printed in any order.
Input: x = 20 Output: 0 1 2 3 4 5 6 7 8 9 10 12 Input: x = 105 Output: 0 1 2 3 4 5 6 7 8 9 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98 101 Note: Order of output doesn't matter, i.e. numbers can be printed in any order
We strongly recommend that you click here and practice it, before moving on to the solution.
One Simple Solution is to traverse all numbers from 0 to x. For every traversed number, check if it is a Jumping number. If yes, then print it. Otherwise ignore it. Time Complexity of this solution is O(x).
Assume that we have a graph where the starting node is 0 and we need to traverse it from the start node to all the reachable nodes.
With the restrictions given in the graph about the jumping numbers, what do you think should be the restrictions defining the next transitions in the graph.
Lets take a example for input x = 90 Start node = 0 From 0, we can move to 1 2 3 4 5 6 7 8 9 [these are not in our range so we don't add it] Now from 1, we can move to 12 and 10 From 2, 23 and 21 From 3, 34 and 32 . . . . . . and so on.
Below is BFS based implementation of above idea.
0 1 10 12 2 21 23 3 32 34 4 5 6 7 8 9
Thanks to Gaurav Ahirwar for above solution.
- Change the above solution to use DFS instead of BFS.
- Extend your solution to print all numbers in sorted order instead of any order.
- Further extend the solution to print all numbers in a given range.
If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
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.
- Minimum numbers (smaller than or equal to N) with sum S
- Euler's Totient function for all numbers smaller than or equal to n
- Print all Strong numbers less than or equal to N
- Print all Semi-Prime Numbers less than or equal to N
- Largest number smaller than or equal to N divisible by K
- Count of smaller or equal elements in sorted array
- Largest number smaller than or equal to n and digits in non-decreasing order
- Sieve of Sundaram to print all primes smaller than n
- Cube Free Numbers smaller than n
- An interesting solution to get all prime numbers smaller than n
- Count of Binary Digit numbers smaller than N
- Find the nearest smaller numbers on left side in an array
- Delete array elements which are smaller than next or become smaller
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Print all pairs in an unsorted array with equal sum
- Print all non-increasing sequences of sum equal to a given number x
- Print equal sum sets of array (Partition Problem) | Set 2
- Print equal sum sets of array (Partition problem) | Set 1