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.
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.
- Delete array elements which are smaller than next or become smaller
- Euler's Totient function for all numbers smaller than or equal to n
- Minimum numbers (smaller than or equal to N) with sum S
- Sieve of Sundaram to print all primes smaller than n
- Count triplets with sum smaller than a given value
- An interesting solution to get all prime numbers smaller than n
- Find the element before which all the elements are smaller than it, and after which all are greater
- Largest number smaller than or equal to n and digits in non-decreasing order
- Largest number smaller than or equal to N divisible by K
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- Print all Semi-Prime Numbers less than or equal to N
- Print all Strong numbers less than or equal to N
- Count of Binary Digit numbers smaller than N
- Cube Free Numbers smaller than n
- Count decrements to nearest smaller element required to make all array elements equal
- Smallest subarray of size greater than K with sum greater than a given value
- Print all proper fractions with denominators less than equal to N
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Count elements less than or equal to a given value in a sorted rotated array