Update appNew update is available. Click here to update.
Last Updated: 8 Jan, 2021
Jumping Numbers
Hard
Problem statement

You are given a positive integer 'N'. Your task is to print all the jumping numbers smaller than or equal to 'N'.

A number is called a jumping number if all adjacent digits in it differ by 1. All the single-digit numbers are considered jumping numbers.

Note:

The difference between β€˜9’ and β€˜0’ is not considered as 1.

Example:

Let’s say 'N' = 25. The jumping numbers less than or equal to 25 are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 21, 23. In all these numbers the adjacent digits differ by 1.
Input Format:
The first line of input contains an integer β€˜T’ representing the number of test cases.

The first and the only line of every test case contains the positive integer β€˜N’.
Output Format:
For each test case, print a single line containing less than or equal to 'N' integers representing all the jumping numbers in sorted order.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 100 
1 <= N <= 10^8

Time Limit: 1 sec
Approaches

01Approach

  • A brute force approach could be to iterate over all the integers from 0 to N.
  • For each integer check if its adjacent digits differ by 1. If yes, then the number is a jumping number.

 

The steps are as follows:

 

  • Iterate over the integers, 0 to β€˜N’.
  • For each integer, iterate over its digits.
    • Check if the current digit and the previous digit differ by 1.
  • If all the adjacent digits in the current integer differ by 1, add the integer to the list of jumping numbers.