Jumping Numbers

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
CodingNinjas
author
2y
Brute Force
  • 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 step...read more

CodingNinjas
author
2y
Using DFS To Generate Jumping Numbers
  • Instead of checking every integer, we can directly generate the jumping numbers.
  • In order to do so, we start with the first jumping number i.e. 0. And append a new ...read more
CodingNinjas
author
2y
Using BFS To Generate Jumping Numbers
  • This approach is similar to the previous one but instead of using DFS, we can use BFS to generate the graph.

The steps are as follows:

  • Initialize an empty queue an...read more
Add answer anonymously...
Red Hat Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter