Javatpoint Logo
Javatpoint Logo

Jumping Number in Java

In this section, we will learn what is jumping number and also create Java programs to check if the given number is a jumping number or not. The jumping number program frequently asked in Java coding tests and academics.

Jumping Number

A number N called a jumping number if all the adjacent digits in N have an absolute difference of 1. Note that the difference between 9 and 0 is not considered as 1. Therefore, all single-digit number is considered as a jumping number.

Jumping Number Example

For example, the number 76789 is a jumping number because each adjacent digit has the difference of 1.

The number 952 is not a jumping number because the adjacent digit has a difference of 4 and 3, respectively.

Jumping Number in Java

Some other jumping numbers are 45676, 212, 121, 123, 210, 212, 232, 234, 321, 323, 343, 345, 432, 434, 454, 456, 543, 545, 565, 567, 654, 656, 676, 678, 765, 767, 787, 789, 876, etc.

Steps to Find Jumping Number

  1. Read or initialize a number N.
  2. Iterate over the integers, 0 to N.
  3. For each integer, iterate over its digits.
    • Check if the current digit and the previous digit differ by 1.
  4. If all the adjacent digits in the current integer differ by 1, add the integer to the list of jumping numbers.

Let's implement the above logic in a Java program.

Jumping Number Java Program

JumpingNumberExample1.java

Output 1:

Enter a number: 121
121 is a jumping number.

Output 2:

Enter a number: 7839
7839 is not a jumping number.

Print All Jumping Numbers Between Given Range

There are two ways to print all jumping numbers within a given range:

  • By Using DFS
  • By Using BFS

By using DFS

In DFS, we do not check every integer, instead we print directly all the jumping numbers. In order to achieve the same, we start with the first jumping number i.e. 0 and append a new digit to it such that the difference between the next and the previous digit is 1.

Note that if the previous digit is 0, the only possible next digit will be 1 that gives the next jumping number i.e. 1. Similarly, for digit 9 the only possible next digit is 8, not 10. But for the numbers 1 to 8, we'll always have two options for next digits (one less and other greater than it).

The above process is repeated with 1. At this point, we have two possible options for the next digit i.e. 2 and 0 that generates the two new jumping numbers i.e. 12 and 10.

In this approach, we assume that the numbers as the nodes of the graph. Let's see the steps implemented in DFS.

  • The above approach can be implemented by assuming the numbers as the nodes of the graph.
  • Starting with node '0', apply DFS on the graph.
  • Base Condition: If the generated node or number is greater than N, stop the traversal for that node and return.
  • Add the current node or number in the jumping list if the number is less than N.
  • Now, to transit from one node to next, append digits to the current node such that the next number generated is also a jumping number and then recursively call DFS on the next node.
  • If we want to generate all the jumping numbers, repeat the above algorithm by taking the remaining integers 2-9 as the starting nodes.
  • At last, sort the list of jumping numbers and return the same.

Let's implement the above approach in a Java program.

JumpingNumberExample2.java

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]

Let's see another logic for the same but in the following program we have not used DFS.

JumpingNumberExample3.java

Output:

Jumping numbers for specified range are: 
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, 121, 123

By Using BFS

The approach is similar to the DFS. But instead of using DFS, we can also use BFS to generate the graph. Let's see the approach.

  1. Initialize an empty queue.
  2. Push the starting node i.e. '0' in the queue.
  3. Repeat the following steps until the queue becomes empty:
    1. Pop the top node from the queue that will be the current node.
    2. If the current node (number) is less than or equal to N, add it to the list of jumping numbers.
    3. And generate the next node by appending digits to the current node such that the next number (node) generated is also a jumping number. And push the number into the queue.
    4. Else, move to the next node in the queue.
  4. In order to generate all the jumping numbers, repeat the above algorithm by taking the remaining integers 2-9 as the starting nodes.
  5. At last, sort the list of jumping numbers and return the same.

Let's implement the above approach in a Java program.

JumpingNumberExample4.java

Output:

Jumping numbers for specified range are: 
0 1 10 12 101 121 123 2 21 23 210 212 232 234 3 32 34 321 323 343 345 4 43 45 432 434 454 456 5 54 56 6 65 67 7 76 78 8 87 89 9 98






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA