A number is called a Jumping Number if all adjacent digits differ by 1.
- The difference between ‘9’ and ‘0’ is not considered 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
Table of Content
[Naive Approach] Traversing and Checking All Numbers - O(x) Time and O(1) Space
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.
#include <bits/stdc++.h>
using namespace std;
void printJumping(int& x)
{
int number, temp, lastDigit;
bool isJumping;
for (number = 0; number <= x; number++)
{
if (number < 10)
{
cout << number << " ";
continue;
}
isJumping = true;
temp = number;
lastDigit = temp % 10;
temp /= 10;
while (temp)
{
if (abs(lastDigit - (temp % 10)) != 1)
{
isJumping = false;
break;
}
lastDigit = temp % 10;
temp /= 10;
}
if (isJumping)
cout << number << " ";
}
}
int main()
{
int x = 105;
printJumping(x);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void printJumping(int x)
{
int number, temp, lastDigit;
int isJumping;
for (number = 0; number <= x; number++)
{
if (number < 10)
{
printf("%d ", number);
continue;
}
isJumping = 1;
temp = number;
lastDigit = temp % 10;
temp /= 10;
while (temp)
{
if (abs(lastDigit - (temp % 10)) != 1)
{
isJumping = 0;
break;
}
lastDigit = temp % 10;
temp /= 10;
}
if (isJumping)
printf("%d ", number);
}
}
int main()
{
int x = 105;
printJumping(x);
return 0;
}
import java.util.*;
class JumpingNumbers {
public static void printJumping(int x) {
int number, temp, lastDigit;
boolean isJumping;
for (number = 0; number <= x; number++) {
if (number < 10) {
System.out.print(number + " ");
continue;
}
isJumping = true;
temp = number;
lastDigit = temp % 10;
temp /= 10;
while (temp != 0) {
if (Math.abs(lastDigit - (temp % 10)) != 1) {
isJumping = false;
break;
}
lastDigit = temp % 10;
temp /= 10;
}
if (isJumping) {
System.out.print(number + " ");
}
}
}
public static void main(String[] args) {
int x = 105;
printJumping(x);
}
}
# Function to print the jumping numbers in the range [0, n]
def print_jumping(x):
# Iterating over all the numbers in the range [0, n]
for number in range(x + 1):
if number < 10:
# All numbers in [0, 9] are jumping numbers
print(number, end=" ")
continue
# Flag to check if the number is a jumping number
is_jumping = True
temp = number
last_digit = temp % 10
temp //= 10
while temp > 0:
if abs(last_digit - (temp % 10)) != 1:
is_jumping = False
break
last_digit = temp % 10
temp //= 10
# Printing the number if it's a jumping number
if is_jumping:
print(number, end=" ")
# Driver Code
x = 105
print_jumping(x)
using System;
class JumpingNumbers
{
static void PrintJumping(int x)
{
int number, temp, lastDigit;
bool isJumping;
for (number = 0; number <= x; number++)
{
if (number < 10)
{
Console.Write(number + " ");
continue;
}
isJumping = true;
temp = number;
lastDigit = temp % 10;
temp /= 10;
while (temp != 0)
{
if (Math.Abs(lastDigit - (temp % 10)) != 1)
{
isJumping = false;
break;
}
lastDigit = temp % 10;
temp /= 10;
}
if (isJumping)
Console.Write(number + " ");
}
}
public static void Main()
{
int x = 105;
PrintJumping(x);
}
}
function printJumping(x) {
let number, temp, lastDigit;
let isJumping;
for (number = 0; number <= x; number++) {
if (number < 10) {
console.log(number, " ");
continue;
}
isJumping = true;
temp = number;
lastDigit = temp % 10;
temp = Math.floor(temp / 10);
while (temp) {
if (Math.abs(lastDigit - (temp % 10)) !== 1) {
isJumping = false;
break;
}
lastDigit = temp % 10;
temp = Math.floor(temp / 10);
}
if (isJumping) {
console.log(number, " ");
}
}
}
let x = 105;
printJumping(x);
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
[Expected Approach] Using BFS - O(k) Time and O(k) Space
The approach uses Breadth-First Search (BFS) to generate Jumping Numbers starting from digits 1 to 9. Each number expands by appending its last digit ±1 (if valid), ensuring adjacent digits differ by 1. The process continues until numbers exceed x, taking O(k) time and space, where k is the count of valid Jumping Numbers less than or equal to x.
// 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 neither 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;
}
// Finds and prints all jumping numbers smaller than or
// equal to x.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// 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 'num' to it
int queue[1000];
int front = 0, rear = 0;
queue[rear++] = num;
// Do BFS starting from num
while (front < rear) {
num = queue[front++];
if (num <= x) {
printf("%d ", num);
int last_dig = num % 10;
// If last digit is 0, append next digit only
if (last_dig == 0)
queue[rear++] = (num * 10) + (last_dig + 1);
// If last digit is 9, append previous digit only
else if (last_dig == 9)
queue[rear++] = (num * 10) + (last_dig - 1);
// If last digit is neither 0 nor 9, append both
// previous and next digits
else {
queue[rear++] = (num * 10) + (last_dig - 1);
queue[rear++] = (num * 10) + (last_dig + 1);
}
}
}
}
// Prints all jumping numbers smaller than or equal to
// a positive number x
void printJumping(int x)
{
printf("0 ");
for (int i = 1; i <= 9 && i <= x; i++)
bfs(x, i);
}
// Driver program
int main()
{
int x = 40;
printJumping(x);
return 0;
}
// Finds and prints all jumping numbers smaller than or
// equal to x.
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
// Prints all jumping numbers smaller than or equal to x starting
// with 'num'. It mainly does BFS starting from 'num'.
public void bfs(int x, int num)
{
// Create a queue and enqueue 'i' to it
Queue<Integer> q = new LinkedList<Integer>();
q.add(num);
// Do BFS starting from i
while (!q.isEmpty()) {
num = q.peek();
q.poll();
if (num <= x) {
System.out.print(num + " ");
int last_digit = num % 10;
// If last digit is 0, append next digit only
if (last_digit == 0) {
q.add((num * 10) + (last_digit + 1));
}
// If last digit is 9, append previous digit only
else if (last_digit == 9) {
q.add((num * 10) + (last_digit - 1));
}
// If last digit is neither 0 nor 9, append both
// previous and next digits
else {
q.add((num * 10) + (last_digit - 1));
q.add((num * 10) + (last_digit + 1));
}
}
}
}
// Prints all jumping numbers smaller than or equal to
// a positive number x
public void printJumping(int x)
{
System.out.print("0 ");
for (int i = 1; i <= 9 && i <= x; i++) {
this.bfs(x, i);
}
}
// Driver program
public static void main(String[] args) throws IOException
{
int x = 40;
GFG obj = new GFG();
obj.printJumping(x);
}
}
# Class queue for use later
class Queue:
def __init__(self):
self.lst = []
def is_empty(self):
return self.lst == []
def enqueue(self, elem):
self.lst.append(elem)
def dequeue(self):
return self.lst.pop(0)
# Prints all jumping numbers smaller than or equal to
# x starting with 'num'. It mainly does BFS starting
# from 'num'.
def bfs(x, num):
# Create a queue and enqueue i to it
q = Queue()
q.enqueue(num)
# Do BFS starting from 1
while (not q.is_empty()):
num = q.dequeue()
if num<= x:
print(str(num), end =' ')
last_dig = num % 10
# If last digit is 0, append next digit only
if last_dig == 0:
q.enqueue((num * 10) + (last_dig + 1))
# If last digit is 9, append previous digit
# only
elif last_dig == 9:
q.enqueue((num * 10) + (last_dig - 1))
# If last digit is neither 0 nor 9, append
# both previous digit and next digit
else:
q.enqueue((num * 10) + (last_dig - 1))
q.enqueue((num * 10) + (last_dig + 1))
# Prints all jumping numbers smaller than or equal to
# a positive number x
def printJumping(x):
print (str(0), end =' ')
for i in range(1, 10):
bfs(x, i)
# Driver Program ( Change value of x as desired )
x = 40
printJumping(x)
# This code is contributed by Saket Modi
// C# program to finds and prints all jumping
// numbers smaller than or equal to x.
using System;
using System.Collections.Generic;
class GFG
{
// Prints all jumping numbers smaller than or
// equal to x starting with 'num'. It mainly
// does BFS starting from 'num'.
public void bfs(int x, int num)
{
// Create a queue and enqueue 'i' to it
Queue<int> q = new Queue<int>();
q.Enqueue(num);
// Do BFS starting from i
while (q.Count!=0)
{
num = q.Peek();
q.Dequeue();
if (num <= x)
{
Console.Write(num + " ");
int last_digit = num % 10;
// If last digit is 0, append next digit only
if (last_digit == 0)
{
q.Enqueue((num * 10) + (last_digit + 1));
}
// If last digit is 9, append previous digit only
else if (last_digit == 9)
{
q.Enqueue((num * 10) + (last_digit - 1));
}
// If last digit is neither 0 nor 9, append both
// previous and next digits
else
{
q.Enqueue((num * 10) + (last_digit - 1));
q.Enqueue((num * 10) + (last_digit + 1));
}
}
}
}
// Prints all jumping numbers smaller than or equal to
// a positive number x
public void printJumping(int x)
{
Console.Write("0 ");
for (int i = 1; i <= 9 && i <= x; i++)
{
this.bfs(x, i);
}
}
// Driver code
public static void Main(String[] args)
{
int x = 40;
GFG obj = new GFG();
obj.printJumping(x);
}
}
// This code has been contributed by 29AjayKumar
// Finds and prints all jumping numbers
// smaller than or equal to x.
// Prints all jumping numbers smaller than
// or equal to x starting with 'num'. It
// mainly does BFS starting from 'num'.
function bfs(x, num)
{
// Create a queue and enqueue 'i' to it
let q = [];
q.push(num);
// Do BFS starting from i
while (q.length != 0)
{
num = q.shift();
if (num <= x)
{
console.log(num + " ");
let last_digit = num % 10;
// If last digit is 0, append next digit only
if (last_digit == 0)
{
q.push((num * 10) + (last_digit + 1));
}
// If last digit is 9, append previous
// digit only
else if (last_digit == 9)
{
q.push((num * 10) + (last_digit - 1));
}
// If last digit is neither 0 nor 9,
// append both previous and next digits
else
{
q.push((num * 10) + (last_digit - 1));
q.push((num * 10) + (last_digit + 1));
}
}
}
}
// Prints all jumping numbers smaller
// than or equal to a positive number x
function printJumping(x)
{
console.log("0 ");
for(let i = 1; i <= 9 && i <= x; i++)
{
bfs(x, i);
}
}
// Driver code
let x = 40;
printJumping(x);
// This code is contributed by rag2127
Output
0 1 10 12 2 21 23 3 32 34 4 5 6 7 8 9
[Alternate Approach] using DFS - O(k) Time and O(k) Space
In the DFS based approach we start building our numbers from single digits , i.e. from 1 - 9. Then we check for next possible digit and if possible we call the dfs for those numbers, increasing the number of digits with each call. The process continues until numbers exceed x, taking O(k) time and space, where k is the count of valid Jumping Numbers less than or equal to x.
// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
#define ll long long
void dfs(ll cnum, const ll& num)
{
if (cnum > num) // base case
return;
cout << cnum << " "; // print the current number
int l_dig = cnum % 10; // get the last digit of current number
// for the next digit we have two options
ll first = cnum * 10 + l_dig + 1;
ll second = cnum * 10 + l_dig - 1;
if (l_dig == 0) // here as second option will give us a
// -ve number we will skip it
dfs(first, num);
else if (l_dig == 9) // here as first option will give
// us a -ve number we will skip it
dfs(second, num);
else // else we call on both the options
{
dfs(first, num);
dfs(second, num);
}
}
void PrintJumping(long long X)
{
cout << 0 << " ";
for (ll i = 1; i <= 9; i++) {
dfs(i, X); // generate all the numbers starting
// from i
}
}
int main()
{
long long X = 40;
PrintJumping(X);
return 0;
}
#include <stdio.h>
void dfs(long long cnum, const long long num)
{
if (cnum > num) // base case
return;
printf("%lld ", cnum); // print the current number
int l_dig = cnum % 10; // get the last digit of current number
// for the next digit we have two options
long long first = cnum * 10 + l_dig + 1;
long long second = cnum * 10 + l_dig - 1;
if (l_dig == 0) // here as second option will give us a
// -ve number we will skip it
dfs(first, num);
else if (l_dig == 9) // here as first option will give
// us a -ve number we will skip it
dfs(second, num);
else // else we call on both the options
{
dfs(first, num);
dfs(second, num);
}
}
void PrintJumping(long long X)
{
printf("0 ");
for (long long i = 1; i <= 9; i++) {
dfs(i, X); // generate all the numbers starting
// from i
}
}
int main()
{
long long X = 40;
PrintJumping(X);
return 0;
}
// Java implementation of the above approach
import java.util.*;
class gfg2 {
static void dfs(long cnum, long num)
{
if (cnum > num) // base case
return;
System.out.print(cnum
+ " "); // print the current number
int l_dig = (int)(cnum % 10); // get the last digit
// of current number
// for the next digit we have two options
long first = cnum * 10 + l_dig + 1;
long second = cnum * 10 + l_dig - 1;
if (l_dig == 0) // here as second option will give
// us a -ve number we will skip it
dfs(first, num);
else if (l_dig
== 9) // here as first option will give
// us a -ve number we will skip it
dfs(second, num);
else // else we call on both the options
{
dfs(first, num);
dfs(second, num);
}
}
static void PrintJumping(long X)
{
System.out.print(0 + " ");
for (long i = 1L; i <= 9; i++) {
dfs(i, X); // generate all the numbers starting
// from i
}
}
public static void main(String[] args)
{
long X = 40;
PrintJumping(X);
}
}
# Python3 implementation of the above approach
def dfs(cnum, num):
# base case
if cnum > num:
return
# print the current number
print(cnum, end=" ")
# get the last digit of the current number
l_dig = cnum % 10
# for the next digit we have two options
first = cnum * 10 + l_dig + 1
second = cnum * 10 + l_dig - 1
# here as second option will give us a -ve number
# we will skip it
if l_dig == 0:
dfs(first, num)
# here as first option will give us a -ve number
# we will skip it
elif l_dig == 9:
dfs(second, num)
# else we will call on both the options
else:
dfs(first, num)
dfs(second, num)
# Print Jumping numbers
def PrintJumping(X):
print(0, end=" ")
for i in range(1, 10):
dfs(i, X)
# Driver code
if __name__ == '__main__':
X = 40
PrintJumping(X)
// C# implementation of the above approach
using System;
class GFG {
static void dfs(long cnum, long num)
{
if (cnum > num) // base case
return;
Console.Write(cnum
+ " "); // print the current number
int l_dig = (int)(cnum % 10); // get the last digit
// of current number
// for the next digit we have two options
long first = cnum * 10 + l_dig + 1;
long second = cnum * 10 + l_dig - 1;
if (l_dig == 0) // here as second option will give
// us a -ve number we will skip it
dfs(first, num);
else if (l_dig
== 9) // here as first option will give
// us a -ve number we will skip it
dfs(second, num);
else // else we call on both the options
{
dfs(first, num);
dfs(second, num);
}
}
static void PrintJumping(long X)
{
Console.Write(0 + " ");
for (long i = 1L; i <= 9; i++) {
dfs(i, X); // generate all the numbers starting
// from i
}
}
static void Main(string[] args)
{
long X = 40;
PrintJumping(X);
}
}
// This code is contributed by karandeep1234
// Javascript implementation of the above approach
function dfs(cnum, num)
{
if (cnum > num) // base case
return;
console.log(cnum+" "); // print the current number
let l_dig = cnum % 10; // get the last digit of current number
// for the next digit we have two options
let first = cnum * 10 + l_dig + 1;
let second = cnum * 10 + l_dig - 1;
if (l_dig == 0) // here as second option will give us a
// -ve number we will skip it
dfs(first, num);
else if (l_dig == 9) // here as first option will give
// us a -ve number we will skip it
dfs(second, num);
else // else we call on both the options
{
dfs(first, num);
dfs(second, num);
}
}
function PrintJumping(X)
{
console.log(0+" ");
for(let i = 1; i <= 9; i++) {
dfs(i, X); // generate all the numbers starting
// from i
}
}
let X = 40;
PrintJumping(X);
Output
0 1 12 10 2 23 21 3 34 32 4 5 6 7 8 9