top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Print all Jumping Numbers smaller than or equal to a given value.

+3 votes
4,430 views

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.

Example:

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

posted Feb 11, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

We strongly recommend you to minimize your browser and try this yourself first.

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).

An Efficient Solution can solve this problem in O(k) time where k is number of Jumping Numbers smaller than or equal to x. The idea is use BFS or DFS.

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 C++ implementation of above idea.

// Finds and prints all jumping numbers smaller than or
// equal to x.
#include<bits/stdc++.h>
using namespace std;

// Prints all jumping numbers smaller than or equal to x starting
// with 'num'. It mainly does BFS starting from 'num'.
void bfs(int x, int num)
{
    // Create a queue and enqueue 'i' to it
    queue<int > q;
    q.push(num);

    // Do BFS starting from i
    while (!q.empty())
    {
        num = q.front();
        q.pop();

        if (num <= x)
        {
            cout << num << " ";
            int last_dig = num % 10;

            // If last digit is 0, append next digit only
            if (last_dig == 0)
                q.push((num*10) + (last_dig+1));


            // If last digit is 9, append previous digit only
            else if (last_dig == 9)
                q.push( (num*10) + (last_dig-1) );

            // If last digit is neighter 0 nor 9, append both 
            // previous and next digits
            else
            {
                q.push((num*10) + (last_dig-1));
                q.push((num*10) + (last_dig+1));
            }
        }
    }
}

// Prints all jumping numbers smaller than or equal to
// a positive number x
void printJumping(int x)
{
    cout << 0 << " ";
    for (int i=1; i<=9 && i<=x; i++)
       bfs(x, i);
}

// Driver program
int main()
{
    int x = 40;
    printJumping(x);
    return 0;
}

Output:

0 1 10 12 2 21 23 3 32 34 4 5 6 7 8 9 
answer Feb 12, 2016 by Shivaranjini
Similar Questions
+5 votes

Algorithm to print all the number possible with the digits of a given number

eg :if input is "234",combinations possible are

234,243,324,342,432,423

0 votes

Suppose I have been given an array and I need to findout all such combination where a^2 + b^2 = c^2 then how can I achieve that?

C/C++ code would be helpful?

+2 votes

How will we print numbers from 1 to N without using loop or recursion?

+2 votes

it is illegal to share some certain prime numbers in US and if you do so,you will get arrested.Follow this link to get more details :
https://www.youtube.com/watch?v=LnEyjwdoj7g
My question is : it is very easy to find multiplication of two prime numbers but tough to get what are the prime numbers which multiplication results this number . Example : 7*11= 77 but you are given 77 = ? * ? where ?= two prime numbers only (ans = 7 and 11).Another example : 11*13= 143 but you need to solve it for 143 and you output : 11 13
So I need a program ,efficient one, to get solutions to such type of mathematical problem.
Adding more examples:
input:1. 21 -> 3,7
2. 189 -> 3,7,9
3.1363 -> 29,47 .... and if is not possible then print -1 example 1. 4 and its factors 2*2 where 2 is repeated so print -1
2. 9-> -1
3. 24->-1 ( prime factors are 2^3 * 3= 8*3 where 8 is not prime so print -1).
Thanks in advance.

+6 votes

First see this example: let you have a number 8 which is divisible by 4 which is square of 2 and 27 which is divisible by 9 which is square of 3.I need a solution for numbers input from 1 to 10^18.hope for efficient program written in any programming language.
Thanks in advance.

...