Jumping Number in JavaIn 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 NumberA 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 singledigit number is considered as a jumping number. Jumping Number ExampleFor 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. 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
Let's implement the above logic in a Java program. Jumping Number Java ProgramJumpingNumberExample1.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 RangeThere are two ways to print all jumping numbers within a given range:
By using DFSIn 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.
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 BFSThe 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.
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
Next TopicLead Number in Java
