Print all N digit jumping numbers
In this article, we will discuss the problem of printing all N-digit jumping numbers. Jumping numbers are numbers where the absolute difference between adjacent digits is always 1. For example, 121 and 3434 are jumping numbers, but 123 and 222 are not.
Problem Statement
Given an integer N, we need to find and print all N-digit jumping numbers. The output should be in ascending order.
Example
Let's understand the problem with an example. Suppose N = 2. The possible jumping numbers of 2 digits are: 10, 12, 21, 23, 32, 34, 43, 45, 54, 56, 65, 67, 76, 78, 87, 89, 98. These numbers have the absolute difference between adjacent digits as 1.
Algorithm
To solve this problem, we can use a recursive approach. Here is the algorithm:
1. Define a function called printSequence() that takes an array of integers and the number of elements to be printed. 2. Inside the printSequence() function, iterate over the array and print each element. 3. Define a recursive function called findCombination() that takes the result array, the current index, and the total number of digits as parameters. 4. In the findCombination() function, check the base cases: - If the current index is equal to the total number of digits, call the printSequence() function to print the current combination and return. - If the current index is greater than the total number of digits, return. 5. If the current index is greater than 0, check the previous digit: - If the previous digit is not 0, set the new digit by reducing the previous digit by one and recursively call findCombination() with the updated index. - If the previous digit is not 9, set the new digit by increasing the previous digit by one and recursively call findCombination() with the updated index. 6. If the current index is 0, iterate from 1 to 9 and set each digit as the first element of the result array. Recursively call findCombination() with the updated index. 7. Define a function called jumpingNumber() that takes an integer N as a parameter. 8. Inside the jumpingNumber() function, check if N is less than or equal to 0. If so, return. 9. Initialize the result array. 10. If N is equal to 1, print 0 as the only jumping number. 11. Call the findCombination() function with the result array, initial index 0, and N as parameters.
Pseudocode
function printSequence(result[], k): for i from 0 to k: print result[i] print new line function findCombination(result[], index, n): if index = n: printSequence(result, index) return if index > n: return if index > 0: if result[index - 1] != 0: result[index] = result[index - 1] - 1 findCombination(result, index + 1, n) if result[index - 1] != 9: result[index] = result[index - 1] + 1 findCombination(result, index + 1, n) else: for i from 1 to 9: result[index] = i findCombination(result, index + 1, n) function jumpingNumber(n): if n ≤ 0: return print "Given n: ", n result = array of size n if n = 1: print 0 findCombination(result, 0, n) jumpingNumber(4)
Code Example
/*
C program for
Print all N digit jumping numbers
*/
#include <stdio.h>
// Display result
void printSequence(int result[], int k)
{
for (int i = 0; i < k; ++i)
{
printf("%d", result[i]);
}
printf("\n ");
}
void findCombination(int result[], int index, int n)
{
if (index == n)
{
// Display calculated result
printSequence(result, index);
return;
}
if (index > n)
{
return;
}
if (index > 0)
{
if (result[index - 1] != 0)
{
// Set new digit by reduce previous digit by one
result[index] = result[index - 1] - 1;
findCombination(result, index + 1, n);
}
if (result[index - 1] != 9)
{
// Set new digit by increase previous digit by one
result[index] = result[index - 1] + 1;
// Find next digit
findCombination(result, index + 1, n);
}
}
else
{
// This is used to setup first element
for (int i = 1; i <= 9; ++i)
{
result[index] = i;
findCombination(result, index + 1, n);
}
}
}
// Handles the request of find combination of given n
void jumpingNumber(int n)
{
if (n <= 0)
{
return;
}
printf("\n Given n : %d \n ", n);
// Collect result
int result[n];
if (n == 1)
{
printf("\n 0 \n ");
}
// Test
findCombination(result, 0, n);
}
int main(int argc, char
const *argv[])
{
// Test
jumpingNumber(4);
return 0;
}
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
/*
Java program
Print all N digit jumping numbers
*/
public class JumpingNumber
{
// Display result
public void printSequence(int[] result, int k)
{
for (int i = 0; i < k; ++i)
{
System.out.print(result[i]);
}
System.out.print("\n ");
}
public void findCombination(int[] result, int index, int n)
{
if (index == n)
{
// Display calculated result
printSequence(result, index);
return;
}
if (index > n)
{
return;
}
if (index > 0)
{
if (result[index - 1] != 0)
{
// Set new digit by reduce previous digit by one
result[index] = result[index - 1] - 1;
findCombination(result, index + 1, n);
}
if (result[index - 1] != 9)
{
// Set new digit by increase previous digit by one
result[index] = result[index - 1] + 1;
// Find next digit
findCombination(result, index + 1, n);
}
}
else
{
// This is used to setup first element
for (int i = 1; i <= 9; ++i)
{
result[index] = i;
findCombination(result, index + 1, n);
}
}
}
// Handles the request of find combination of given n
public void jumpingNo(int n)
{
if (n <= 0)
{
return;
}
System.out.print("\n Given n : " + n + " \n ");
// Collect result
int[] result = new int[n];
if (n == 1)
{
System.out.print("\n 0 \n ");
}
// Test
findCombination(result, 0, n);
}
public static void main(String[] args)
{
JumpingNumber task = new JumpingNumber();
// Test
task.jumpingNo(4);
}
}
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
// Include header file
#include <iostream>
using namespace std;
/*
C++ program
Print all N digit jumping numbers
*/
class JumpingNumber
{
public:
// Display result
void printSequence(int result[], int k)
{
for (int i = 0; i < k; ++i)
{
cout << result[i];
}
cout << "\n ";
}
void findCombination(int result[], int index, int n)
{
if (index == n)
{
// Display calculated result
this->printSequence(result, index);
return;
}
if (index > n)
{
return;
}
if (index > 0)
{
if (result[index - 1] != 0)
{
// Set new digit by reduce previous digit by one
result[index] = result[index - 1] - 1;
this->findCombination(result, index + 1, n);
}
if (result[index - 1] != 9)
{
// Set new digit by increase previous digit by one
result[index] = result[index - 1] + 1;
// Find next digit
this->findCombination(result, index + 1, n);
}
}
else
{
// This is used to setup first element
for (int i = 1; i <= 9; ++i)
{
result[index] = i;
this->findCombination(result, index + 1, n);
}
}
}
// Handles the request of find combination of given n
void jumpingNo(int n)
{
if (n <= 0)
{
return;
}
cout << "\n Given n : " << n << " \n ";
// Collect result
int result[n];
if (n == 1)
{
cout << "\n 0 \n ";
}
// Test
this->findCombination(result, 0, n);
}
};
int main()
{
JumpingNumber *task = new JumpingNumber();
// Test
task->jumpingNo(4);
return 0;
}
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
// Include namespace system
using System;
/*
Csharp program
Print all N digit jumping numbers
*/
public class JumpingNumber
{
// Display result
public void printSequence(int[] result, int k)
{
for (int i = 0; i < k; ++i)
{
Console.Write(result[i]);
}
Console.Write("\n ");
}
public void findCombination(int[] result, int index, int n)
{
if (index == n)
{
// Display calculated result
this.printSequence(result, index);
return;
}
if (index > n)
{
return;
}
if (index > 0)
{
if (result[index - 1] != 0)
{
// Set new digit by reduce previous digit by one
result[index] = result[index - 1] - 1;
this.findCombination(result, index + 1, n);
}
if (result[index - 1] != 9)
{
// Set new digit by increase previous digit by one
result[index] = result[index - 1] + 1;
// Find next digit
this.findCombination(result, index + 1, n);
}
}
else
{
// This is used to setup first element
for (int i = 1; i <= 9; ++i)
{
result[index] = i;
this.findCombination(result, index + 1, n);
}
}
}
// Handles the request of find combination of given n
public void jumpingNo(int n)
{
if (n <= 0)
{
return;
}
Console.Write("\n Given n : " + n + " \n ");
// Collect result
int[] result = new int[n];
if (n == 1)
{
Console.Write("\n 0 \n ");
}
// Test
this.findCombination(result, 0, n);
}
public static void Main(String[] args)
{
JumpingNumber task = new JumpingNumber();
// Test
task.jumpingNo(4);
}
}
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
package main
import "fmt"
/*
Go program
Print all N digit jumping numbers
*/
type JumpingNumber struct {}
func getJumpingNumber() * JumpingNumber {
var me *JumpingNumber = &JumpingNumber {}
return me
}
// Display result
func(this JumpingNumber) printSequence(result[] int, k int) {
for i := 0 ; i < k ; i++ {
fmt.Print(result[i])
}
fmt.Print("\n ")
}
func(this JumpingNumber) findCombination(result[] int, index int, n int) {
if index == n {
// Display calculated result
this.printSequence(result, index)
return
}
if index > n {
return
}
if index > 0 {
if result[index - 1] != 0 {
// Set new digit by reduce previous digit by one
result[index] = result[index - 1] - 1
this.findCombination(result, index + 1, n)
}
if result[index - 1] != 9 {
// Set new digit by increase previous digit by one
result[index] = result[index - 1] + 1
// Find next digit
this.findCombination(result, index + 1, n)
}
} else {
// This is used to setup first element
for i := 1 ; i <= 9 ; i++ {
result[index] = i
this.findCombination(result, index + 1, n)
}
}
}
// Handles the request of find combination of given n
func(this JumpingNumber) jumpingNo(n int) {
if n <= 0 {
return
}
fmt.Print("\n Given n : ", n, " \n ")
// Collect result
var result = make([] int, n)
if n == 1 {
fmt.Print("\n 0 \n ")
}
// Test
this.findCombination(result, 0, n)
}
func main() {
var task * JumpingNumber = getJumpingNumber()
// Test
task.jumpingNo(4)
}
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
<?php
/*
Php program
Print all N digit jumping numbers
*/
class JumpingNumber
{
// Display result
public function printSequence($result, $k)
{
for ($i = 0; $i < $k; ++$i)
{
echo($result[$i]);
}
echo("\n ");
}
public function findCombination($result, $index, $n)
{
if ($index == $n)
{
// Display calculated result
$this->printSequence($result, $index);
return;
}
if ($index > $n)
{
return;
}
if ($index > 0)
{
if ($result[$index - 1] != 0)
{
// Set new digit by reduce previous digit by one
$result[$index] = $result[$index - 1] - 1;
$this->findCombination($result, $index + 1, $n);
}
if ($result[$index - 1] != 9)
{
// Set new digit by increase previous digit by one
$result[$index] = $result[$index - 1] + 1;
// Find next digit
$this->findCombination($result, $index + 1, $n);
}
}
else
{
// This is used to setup first element
for ($i = 1; $i <= 9; ++$i)
{
$result[$index] = $i;
$this->findCombination($result, $index + 1, $n);
}
}
}
// Handles the request of find combination of given n
public function jumpingNo($n)
{
if ($n <= 0)
{
return;
}
echo("\n Given n : ".$n.
" \n ");
// Collect result
$result = array_fill(0, $n, 0);
if ($n == 1)
{
echo("\n 0 \n ");
}
// Test
$this->findCombination($result, 0, $n);
}
}
function main()
{
$task = new JumpingNumber();
// Test
$task->jumpingNo(4);
}
main();
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
/*
Node JS program
Print all N digit jumping numbers
*/
class JumpingNumber
{
// Display result
printSequence(result, k)
{
for (var i = 0; i < k; ++i)
{
process.stdout.write("" + result[i]);
}
process.stdout.write("\n ");
}
findCombination(result, index, n)
{
if (index == n)
{
// Display calculated result
this.printSequence(result, index);
return;
}
if (index > n)
{
return;
}
if (index > 0)
{
if (result[index - 1] != 0)
{
// Set new digit by reduce previous digit by one
result[index] = result[index - 1] - 1;
this.findCombination(result, index + 1, n);
}
if (result[index - 1] != 9)
{
// Set new digit by increase previous digit by one
result[index] = result[index - 1] + 1;
// Find next digit
this.findCombination(result, index + 1, n);
}
}
else
{
// This is used to setup first element
for (var i = 1; i <= 9; ++i)
{
result[index] = i;
this.findCombination(result, index + 1, n);
}
}
}
// Handles the request of find combination of given n
jumpingNo(n)
{
if (n <= 0)
{
return;
}
process.stdout.write("\n Given n : " + n + " \n ");
// Collect result
var result = Array(n).fill(0);
if (n == 1)
{
process.stdout.write("\n 0 \n ");
}
// Test
this.findCombination(result, 0, n);
}
}
function main()
{
var task = new JumpingNumber();
// Test
task.jumpingNo(4);
}
main();
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
# Python 3 program
# Print all N digit jumping numbers
class JumpingNumber :
# Display result
def printSequence(self, result, k) :
i = 0
while (i < k) :
print(result[i], end = "")
i += 1
print("\n ", end = "")
def findCombination(self, result, index, n) :
if (index == n) :
# Display calculated result
self.printSequence(result, index)
return
if (index > n) :
return
if (index > 0) :
if (result[index - 1] != 0) :
# Set new digit by reduce previous digit by one
result[index] = result[index - 1] - 1
self.findCombination(result, index + 1, n)
if (result[index - 1] != 9) :
# Set new digit by increase previous digit by one
result[index] = result[index - 1] + 1
# Find next digit
self.findCombination(result, index + 1, n)
else :
i = 1
# This is used to setup first element
while (i <= 9) :
result[index] = i
self.findCombination(result, index + 1, n)
i += 1
# Handles the request of find combination of given n
def jumpingNo(self, n) :
if (n <= 0) :
return
print("\n Given n : ", n ," \n ", end = "")
# Collect result
result = [0] * (n)
if (n == 1) :
print("\n 0 \n ", end = "")
# Test
self.findCombination(result, 0, n)
def main() :
task = JumpingNumber()
# Test
task.jumpingNo(4)
if __name__ == "__main__": main()
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
# Ruby program
# Print all N digit jumping numbers
class JumpingNumber
# Display result
def printSequence(result, k)
i = 0
while (i < k)
print(result[i])
i += 1
end
print("\n ")
end
def findCombination(result, index, n)
if (index == n)
# Display calculated result
self.printSequence(result, index)
return
end
if (index > n)
return
end
if (index > 0)
if (result[index - 1] != 0)
# Set new digit by reduce previous digit by one
result[index] = result[index - 1] - 1
self.findCombination(result, index + 1, n)
end
if (result[index - 1] != 9)
# Set new digit by increase previous digit by one
result[index] = result[index - 1] + 1
# Find next digit
self.findCombination(result, index + 1, n)
end
else
i = 1
# This is used to setup first element
while (i <= 9)
result[index] = i
self.findCombination(result, index + 1, n)
i += 1
end
end
end
# Handles the request of find combination of given n
def jumpingNo(n)
if (n <= 0)
return
end
print("\n Given n : ", n ," \n ")
# Collect result
result = Array.new(n) {0}
if (n == 1)
print("\n 0 \n ")
end
# Test
self.findCombination(result, 0, n)
end
end
def main()
task = JumpingNumber.new()
# Test
task.jumpingNo(4)
end
main()
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
/*
Scala program
Print all N digit jumping numbers
*/
class JumpingNumber()
{
// Display result
def printSequence(result: Array[Int], k: Int): Unit = {
var i: Int = 0;
while (i < k)
{
print(result(i));
i += 1;
}
print("\n ");
}
def findCombination(result: Array[Int], index: Int, n: Int): Unit = {
if (index == n)
{
// Display calculated result
printSequence(result, index);
return;
}
if (index > n)
{
return;
}
if (index > 0)
{
if (result(index - 1) != 0)
{
// Set new digit by reduce previous digit by one
result(index) = result(index - 1) - 1;
findCombination(result, index + 1, n);
}
if (result(index - 1) != 9)
{
// Set new digit by increase previous digit by one
result(index) = result(index - 1) + 1;
// Find next digit
findCombination(result, index + 1, n);
}
}
else
{
var i: Int = 1;
// This is used to setup first element
while (i <= 9)
{
result(index) = i;
findCombination(result, index + 1, n);
i += 1;
}
}
}
// Handles the request of find combination of given n
def jumpingNo(n: Int): Unit = {
if (n <= 0)
{
return;
}
print("\n Given n : " + n + " \n ");
// Collect result
var result: Array[Int] = Array.fill[Int](n)(0);
if (n == 1)
{
print("\n 0 \n ");
}
// Test
findCombination(result, 0, n);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: JumpingNumber = new JumpingNumber();
// Test
task.jumpingNo(4);
}
}
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
/*
Swift 4 program
Print all N digit jumping numbers
*/
class JumpingNumber
{
// Display result
func printSequence(_ result: [Int], _ k: Int)
{
var i: Int = 0;
while (i < k)
{
print(result[i], terminator: "");
i += 1;
}
print("\n ", terminator: "");
}
func findCombination(_ result: inout[Int], _ index: Int, _ n: Int)
{
if (index == n)
{
// Display calculated result
self.printSequence(result, index);
return;
}
if (index > n)
{
return;
}
if (index > 0)
{
if (result[index - 1] != 0)
{
// Set new digit by reduce previous digit by one
result[index] = result[index - 1] - 1;
self.findCombination(&result, index + 1, n);
}
if (result[index - 1] != 9)
{
// Set new digit by increase previous digit by one
result[index] = result[index - 1] + 1;
// Find next digit
self.findCombination(&result, index + 1, n);
}
}
else
{
var i: Int = 1;
// This is used to setup first element
while (i <= 9)
{
result[index] = i;
self.findCombination(&result, index + 1, n);
i += 1;
}
}
}
// Handles the request of find combination of given n
func jumpingNo(_ n: Int)
{
if (n <= 0)
{
return;
}
print("\n Given n : ", n ," \n ", terminator: "");
// Collect result
var result: [Int] = Array(repeating: 0, count: n);
if (n == 1)
{
print("\n 0 \n ", terminator: "");
}
// Test
self.findCombination(&result, 0, n);
}
}
func main()
{
let task: JumpingNumber = JumpingNumber();
// Test
task.jumpingNo(4);
}
main();
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
/*
Kotlin program
Print all N digit jumping numbers
*/
class JumpingNumber
{
// Display result
fun printSequence(result: Array < Int > , k: Int): Unit
{
var i: Int = 0;
while (i < k)
{
print(result[i]);
i += 1;
}
print("\n ");
}
fun findCombination(result: Array < Int > , index: Int, n: Int): Unit
{
if (index == n)
{
// Display calculated result
this.printSequence(result, index);
return;
}
if (index > n)
{
return;
}
if (index > 0)
{
if (result[index - 1] != 0)
{
// Set new digit by reduce previous digit by one
result[index] = result[index - 1] - 1;
this.findCombination(result, index + 1, n);
}
if (result[index - 1] != 9)
{
// Set new digit by increase previous digit by one
result[index] = result[index - 1] + 1;
// Find next digit
this.findCombination(result, index + 1, n);
}
}
else
{
var i: Int = 1;
// This is used to setup first element
while (i <= 9)
{
result[index] = i;
this.findCombination(result, index + 1, n);
i += 1;
}
}
}
// Handles the request of find combination of given n
fun jumpingNo(n: Int): Unit
{
if (n <= 0)
{
return;
}
print("\n Given n : " + n + " \n ");
// Collect result
val result: Array < Int > = Array(n)
{
0
};
if (n == 1)
{
print("\n 0 \n ");
}
// Test
this.findCombination(result, 0, n);
}
}
fun main(args: Array < String > ): Unit
{
val task: JumpingNumber = JumpingNumber();
// Test
task.jumpingNo(4);
}
Output
Given n : 4
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
Explanation
The above code uses a recursive approach to find and print all N-digit jumping numbers. It starts by checking the base cases: if the current index is equal to N, it calls the printSequence() function to print the result, and if the current index is greater than N, it returns.
If the current index is greater than 0, it checks the previous digit. If the previous digit is not 0, it sets the new digit by reducing the previous digit by one and recursively calls the findCombination() function with the updated index. Similarly, if the previous digit is not 9, it sets the new digit by increasing the previous digit by one and recursively calls the findCombination() function with the updated index.
If the current index is 0, it means we are setting up the first element. In this case, it iterates from 1 to 9 and sets each digit as the first element of the result array. It then recursively calls the findCombination() function with the updated index.
The jumpingNumber() function handles the request to find and print all N-digit jumping numbers. It first checks if N is less than or equal to 0, and if so, it returns. Then, it initializes the result array. If N is equal to 1, it prints 0 as the only jumping number. Finally, it calls the findCombination() function with the result array, initial index 0, and N as parameters.
In the provided example, the code is executed with N = 4. The output is a list of all 4-digit jumping numbers. Each number represents a sequence of digits, where the absolute difference between adjacent digits is always 1.
The time complexity of the code depends on the number of jumping numbers generated for a given value of N. As the number of jumping numbers increases exponentially with N, the time complexity can be approximated as O(2^N). However, it's important to note that the actual time complexity may vary based on implementation details and the specific environment in which the code is executed.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment