Posted on by Kalkicode
Code Queue

Print all jumping numbers in ascending order by given limits

The problem of "Print all Jumping Numbers in Ascending Order by Given Limits" involves finding and printing all the jumping numbers within a given limit. A jumping number is a number where the difference between adjacent digits is either 1 or -1. The goal is to generate and display all such numbers within the specified range in ascending order.

Problem Statement

Given a limit num, we need to find and print all jumping numbers from 0 to num in ascending order.

Example

Let's consider the limit num as 50. The jumping numbers from 0 to 50 are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 21, 23, 32, 34, 43, 45.

Idea to Solve

To solve this problem, we can follow these steps:

  1. Initialize a queue data structure to store jumping numbers.
  2. Start by enqueueing all single-digit numbers from 0 to 9.
  3. While the queue is not empty, dequeue the front element, check its last digit, and generate the next jumping numbers based on adjacent differences of 1 or -1.
  4. Enqueue the generated numbers that are within the given limit.
  5. Repeat this process until the queue becomes empty.

Algorithm

  1. Create a class QNode with data and next attributes.
  2. Create a class MyQueue with front, rear, and size attributes, along with methods for enqueue, dequeue, size check, and empty check.
  3. In the Jumping class:
    • Implement JumpingSteps(num) method:
      • Enqueue single-digit numbers from 0 to 9.
      • While the queue is not empty, dequeue the front element, get its last digit, and generate new jumping numbers.
      • Enqueue valid generated numbers within the limit.
  4. In the main() method, create an instance of the Jumping class and call the JumpingSteps() method with the desired limit.

Code Solution

/*
    Java program
    Print all jumping numbers in ascending order by given limits
*/
class QNode
{
    public int data;
    public QNode next;
    public QNode(int data)
    {
        this.data = data;
        this.next = null;
    }
}
// Define custom queue class
class MyQueue
{
    public QNode front;
    public QNode rear;
    public int size;
    public MyQueue()
    {
        this.front = null;
        this.rear = null;
        this.size = 0;
    }
    // Add a new node at last of queue
    public void enqueue(int data)
    {
        QNode node = new QNode(data);
        if (this.front == null)
        {
            // When first node of queue
            this.front = node;
        }
        else
        {
            // Add node at last level
            this.rear.next = node;
        }
        this.size++;
        this.rear = node;
    }
    // Delete front node of queue
    public void dequeue()
    {
        if (this.front != null)
        {
            if (this.rear == this.front)
            {
                this.rear = null;
                this.front = null;
            }
            else
            {
                this.front = this.front.next;
            }
            this.size--;
        }
    }
    public int isSize()
    {
        return this.size;
    }
    public boolean isEmpty()
    {
        if (this.isSize() == 0)
        {
            return true;
        }
        return false;
    }
    public QNode peek()
    {
        if (this.isSize() == 0)
        {
            return null;
        }
        else
        {
            return this.front;
        }
    }
}
public class Jumping
{
    public void JumpingSteps(int num)
    {
        if (num < 0)
        {
            return;
        }
        int v = 0;
        if (num <= 10)
        {
            while (v <= num)
            {
                System.out.print("  " + v);
                v++;
            }
            return;
        }
        MyQueue record = new MyQueue();
        int count = 1;
        int digit = 0;
        System.out.print("  0");
        while (count <= num || record.isEmpty() == false)
        {
            if (count <= num)
            {
                if (count <= 9)
                {
                    System.out.print("  " + count);
                    record.enqueue(count);
                    count++;
                }
                else
                {
                    // Get head value
                    v = record.peek().data;
                    // Remove head node
                    record.dequeue();
                    // Find last digit
                    digit = v % 10;
                    if (digit != 0)
                    {
                        // Make new number
                        count = (v * 10) + (digit - 1);
                        if (count <= num)
                        {
                            System.out.print("  " + count);
                            record.enqueue(count);
                        }
                    }
                    if (digit != 9 && count <= num)
                    {
                        // Make new number
                        count = (v * 10) + (digit + 1);
                        if (count <= num)
                        {
                            System.out.print("  " + count);
                            record.enqueue(count);
                        }
                    }
                }
            }
            else
            {
                record.dequeue();
            }
        }
    }
    public static void main(String[] args)
    {
        Jumping task = new Jumping();
        // 0..1400
        task.JumpingSteps(1400);
    }
}

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  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  878  898  987  989  1010  1012  1210  1212  1232  1234
// Include header file
#include <iostream>
using namespace std;
/*
    C++ program
    Print all jumping numbers in ascending order by given limits
*/
class QNode
{
    public: int data;
    QNode *next;
    QNode(int data)
    {
        this->data = data;
        this->next = NULL;
    }
};
// Define custom queue class
class MyQueue
{
    public: QNode *front;
    QNode *rear;
    int size;
    MyQueue()
    {
        this->front = NULL;
        this->rear = NULL;
        this->size = 0;
    }
    // Add a new node at last of queue
    void enqueue(int data)
    {
        QNode *node = new QNode(data);
        if (this->front == NULL)
        {
            // When first node of queue
            this->front = node;
        }
        else
        {
            // Add node at last level
            this->rear->next = node;
        }
        this->size++;
        this->rear = node;
    }
    // Delete front node of queue
    void dequeue()
    {
        if (this->front != NULL)
        {
            if (this->rear == this->front)
            {
                this->rear = NULL;
                this->front = NULL;
            }
            else
            {
                this->front = this->front->next;
            }
            this->size--;
        }
    }
    int isSize()
    {
        return this->size;
    }
    bool isEmpty()
    {
        if (this->isSize() == 0)
        {
            return true;
        }
        return false;
    }
    QNode *peek()
    {
        if (this->isSize() == 0)
        {
            return NULL;
        }
        else
        {
            return this->front;
        }
    }
};
class Jumping
{
    public: void JumpingSteps(int num)
    {
        if (num < 0)
        {
            return;
        }
        int v = 0;
        if (num <= 10)
        {
            while (v <= num)
            {
                cout << "  " << v;
                v++;
            }
            return;
        }
        MyQueue *record = new MyQueue();
        int count = 1;
        int digit = 0;
        cout << "  0";
        while (count <= num || record->isEmpty() == false)
        {
            if (count <= num)
            {
                if (count <= 9)
                {
                    cout << "  " << count;
                    record->enqueue(count);
                    count++;
                }
                else
                {
                    // Get head value
                    v = record->peek()->data;
                    // Remove head node
                    record->dequeue();
                    // Find last digit
                    digit = v % 10;
                    if (digit != 0)
                    {
                        // Make new number
                        count = (v *10) + (digit - 1);
                        if (count <= num)
                        {
                            cout << "  " << count;
                            record->enqueue(count);
                        }
                    }
                    if (digit != 9 && count <= num)
                    {
                        // Make new number
                        count = (v *10) + (digit + 1);
                        if (count <= num)
                        {
                            cout << "  " << count;
                            record->enqueue(count);
                        }
                    }
                }
            }
            else
            {
                record->dequeue();
            }
        }
    }
};
int main()
{
    Jumping *task = new Jumping();
    // 0..1400
    task->JumpingSteps(1400);
    return 0;
}

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  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  878  898  987  989  1010  1012  1210  1212  1232  1234
// Include namespace system
using System;
/*
    Csharp program
    Print all jumping numbers in ascending order by given limits
*/
public class QNode
{
    public int data;
    public QNode next;
    public QNode(int data)
    {
        this.data = data;
        this.next = null;
    }
}
// Define custom queue class
public class MyQueue
{
    public QNode front;
    public QNode rear;
    public int size;
    public MyQueue()
    {
        this.front = null;
        this.rear = null;
        this.size = 0;
    }
    // Add a new node at last of queue
    public void enqueue(int data)
    {
        QNode node = new QNode(data);
        if (this.front == null)
        {
            // When first node of queue
            this.front = node;
        }
        else
        {
            // Add node at last level
            this.rear.next = node;
        }
        this.size++;
        this.rear = node;
    }
    // Delete front node of queue
    public void dequeue()
    {
        if (this.front != null)
        {
            if (this.rear == this.front)
            {
                this.rear = null;
                this.front = null;
            }
            else
            {
                this.front = this.front.next;
            }
            this.size--;
        }
    }
    public int isSize()
    {
        return this.size;
    }
    public Boolean isEmpty()
    {
        if (this.isSize() == 0)
        {
            return true;
        }
        return false;
    }
    public QNode peek()
    {
        if (this.isSize() == 0)
        {
            return null;
        }
        else
        {
            return this.front;
        }
    }
}
public class Jumping
{
    public void JumpingSteps(int num)
    {
        if (num < 0)
        {
            return;
        }
        int v = 0;
        if (num <= 10)
        {
            while (v <= num)
            {
                Console.Write("  " + v);
                v++;
            }
            return;
        }
        MyQueue record = new MyQueue();
        int count = 1;
        int digit = 0;
        Console.Write("  0");
        while (count <= num || record.isEmpty() == false)
        {
            if (count <= num)
            {
                if (count <= 9)
                {
                    Console.Write("  " + count);
                    record.enqueue(count);
                    count++;
                }
                else
                {
                    // Get head value
                    v = record.peek().data;
                    // Remove head node
                    record.dequeue();
                    // Find last digit
                    digit = v % 10;
                    if (digit != 0)
                    {
                        // Make new number
                        count = (v * 10) + (digit - 1);
                        if (count <= num)
                        {
                            Console.Write("  " + count);
                            record.enqueue(count);
                        }
                    }
                    if (digit != 9 && count <= num)
                    {
                        // Make new number
                        count = (v * 10) + (digit + 1);
                        if (count <= num)
                        {
                            Console.Write("  " + count);
                            record.enqueue(count);
                        }
                    }
                }
            }
            else
            {
                record.dequeue();
            }
        }
    }
    public static void Main(String[] args)
    {
        Jumping task = new Jumping();
        // 0..1400
        task.JumpingSteps(1400);
    }
}

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  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  878  898  987  989  1010  1012  1210  1212  1232  1234
package main
import "fmt"
/*
    Go program
    Print all jumping numbers in ascending order by given limits
*/
type QNode struct {
    data int
    next * QNode
}
func getQNode(data int) * QNode {
    var me *QNode = &QNode {}
    me.data = data
    me.next = nil
    return me
}
// Define custom queue class
type MyQueue struct {
    front * QNode
    rear * QNode
    size int
}
func getMyQueue() * MyQueue {
    var me *MyQueue = &MyQueue {}
    me.front = nil
    me.rear = nil
    me.size = 0
    return me
}
// Add a new node at last of queue
func(this *MyQueue) enqueue(data int) {
    var node * QNode = getQNode(data)
    if this.front == nil {
        // When first node of queue
        this.front = node
    } else {
        // Add node at last level
        this.rear.next = node
    }
    this.size++
    this.rear = node
}
// Delete front node of queue
func(this *MyQueue) dequeue() {
    if this.front != nil {
        if this.rear == this.front {
            this.rear = nil
            this.front = nil
        } else {
            this.front = this.front.next
        }
        this.size--
    }
}
func(this MyQueue) isSize() int {
    return this.size
}
func(this MyQueue) isEmpty() bool {
    if this.isSize() == 0 {
        return true
    }
    return false
}
func(this MyQueue) peek() * QNode {
    if this.isSize() == 0 {
        return nil
    } else {
        return this.front
    }
}
type Jumping struct {}
func getJumping() * Jumping {
    var me *Jumping = &Jumping {}
    return me
}
func(this Jumping) JumpingSteps(num int) {
    if num < 0 {
        return
    }
    var v int = 0
    if num <= 10 {
        for (v <= num) {
            fmt.Print("  ", v)
            v++
        }
        return
    }
    var record * MyQueue = getMyQueue()
    var count int = 1
    var digit int = 0
    fmt.Print("  0")
    for (count <= num || record.isEmpty() == false) {
        if count <= num {
            if count <= 9 {
                fmt.Print("  ", count)
                record.enqueue(count)
                count++
            } else {
                // Get head value
                v = record.peek().data
                // Remove head node
                record.dequeue()
                // Find last digit
                digit = v % 10
                if digit != 0 {
                    // Make new number
                    count = (v * 10) + (digit - 1)
                    if count <= num {
                        fmt.Print("  ", count)
                        record.enqueue(count)
                    }
                }
                if digit != 9 && count <= num {
                    // Make new number
                    count = (v * 10) + (digit + 1)
                    if count <= num {
                        fmt.Print("  ", count)
                        record.enqueue(count)
                    }
                }
            }
        } else {
            record.dequeue()
        }
    }
}
func main() {
    var task * Jumping = getJumping()
    // 0..1400
    task.JumpingSteps(1400)
}

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  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  878  898  987  989  1010  1012  1210  1212  1232  1234
<?php
/*
    Php program
    Print all jumping numbers in ascending order by given limits
*/
class QNode
{
    public $data;
    public $next;
    public  function __construct($data)
    {
        $this->data = $data;
        $this->next = NULL;
    }
}
// Define custom queue class
class MyQueue
{
    public $front;
    public $rear;
    public $size;
    public  function __construct()
    {
        $this->front = NULL;
        $this->rear = NULL;
        $this->size = 0;
    }
    // Add a new node at last of queue
    public  function enqueue($data)
    {
        $node = new QNode($data);
        if ($this->front == NULL)
        {
            // When first node of queue
            $this->front = $node;
        }
        else
        {
            // Add node at last level
            $this->rear->next = $node;
        }
        $this->size++;
        $this->rear = $node;
    }
    // Delete front node of queue
    public  function dequeue()
    {
        if ($this->front != NULL)
        {
            if ($this->rear == $this->front)
            {
                $this->rear = NULL;
                $this->front = NULL;
            }
            else
            {
                $this->front = $this->front->next;
            }
            $this->size--;
        }
    }
    public  function isSize()
    {
        return $this->size;
    }
    public  function isEmpty()
    {
        if ($this->isSize() == 0)
        {
            return true;
        }
        return false;
    }
    public  function peek()
    {
        if ($this->isSize() == 0)
        {
            return NULL;
        }
        else
        {
            return $this->front;
        }
    }
}
class Jumping
{
    public  function JumpingSteps($num)
    {
        if ($num < 0)
        {
            return;
        }
        $v = 0;
        if ($num <= 10)
        {
            while ($v <= $num)
            {
                echo("  ".$v);
                $v++;
            }
            return;
        }
        $record = new MyQueue();
        $count = 1;
        $digit = 0;
        echo("  0");
        while ($count <= $num || $record->isEmpty() == false)
        {
            if ($count <= $num)
            {
                if ($count <= 9)
                {
                    echo("  ".$count);
                    $record->enqueue($count);
                    $count++;
                }
                else
                {
                    // Get head value
                    $v = $record->peek()->data;
                    // Remove head node
                    $record->dequeue();
                    // Find last digit
                    $digit = $v % 10;
                    if ($digit != 0)
                    {
                        // Make new number
                        $count = ($v * 10) + ($digit - 1);
                        if ($count <= $num)
                        {
                            echo("  ".$count);
                            $record->enqueue($count);
                        }
                    }
                    if ($digit != 9 && $count <= $num)
                    {
                        // Make new number
                        $count = ($v * 10) + ($digit + 1);
                        if ($count <= $num)
                        {
                            echo("  ".$count);
                            $record->enqueue($count);
                        }
                    }
                }
            }
            else
            {
                $record->dequeue();
            }
        }
    }
}

function main()
{
    $task = new Jumping();
    // 0..1400
    $task->JumpingSteps(1400);
}
main();

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  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  878  898  987  989  1010  1012  1210  1212  1232  1234
/*
    Node JS program
    Print all jumping numbers in ascending order by given limits
*/
class QNode
{
    constructor(data)
    {
        this.data = data;
        this.next = null;
    }
}
// Define custom queue class
class MyQueue
{
    constructor()
    {
        this.front = null;
        this.rear = null;
        this.size = 0;
    }
    // Add a new node at last of queue
    enqueue(data)
    {
        var node = new QNode(data);
        if (this.front == null)
        {
            // When first node of queue
            this.front = node;
        }
        else
        {
            // Add node at last level
            this.rear.next = node;
        }
        this.size++;
        this.rear = node;
    }
    // Delete front node of queue
    dequeue()
    {
        if (this.front != null)
        {
            if (this.rear == this.front)
            {
                this.rear = null;
                this.front = null;
            }
            else
            {
                this.front = this.front.next;
            }
            this.size--;
        }
    }
    isSize()
    {
        return this.size;
    }
    isEmpty()
    {
        if (this.isSize() == 0)
        {
            return true;
        }
        return false;
    }
    peek()
    {
        if (this.isSize() == 0)
        {
            return null;
        }
        else
        {
            return this.front;
        }
    }
}
class Jumping
{
    JumpingSteps(num)
    {
        if (num < 0)
        {
            return;
        }
        var v = 0;
        if (num <= 10)
        {
            while (v <= num)
            {
                process.stdout.write("  " + v);
                v++;
            }
            return;
        }
        var record = new MyQueue();
        var count = 1;
        var digit = 0;
        process.stdout.write("  0");
        while (count <= num || record.isEmpty() == false)
        {
            if (count <= num)
            {
                if (count <= 9)
                {
                    process.stdout.write("  " + count);
                    record.enqueue(count);
                    count++;
                }
                else
                {
                    // Get head value
                    v = record.peek().data;
                    // Remove head node
                    record.dequeue();
                    // Find last digit
                    digit = v % 10;
                    if (digit != 0)
                    {
                        // Make new number
                        count = (v * 10) + (digit - 1);
                        if (count <= num)
                        {
                            process.stdout.write("  " + count);
                            record.enqueue(count);
                        }
                    }
                    if (digit != 9 && count <= num)
                    {
                        // Make new number
                        count = (v * 10) + (digit + 1);
                        if (count <= num)
                        {
                            process.stdout.write("  " + count);
                            record.enqueue(count);
                        }
                    }
                }
            }
            else
            {
                record.dequeue();
            }
        }
    }
}

function main()
{
    var task = new Jumping();
    // 0..1400
    task.JumpingSteps(1400);
}
main();

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  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  878  898  987  989  1010  1012  1210  1212  1232  1234
#    Python 3 program
#    Print all jumping numbers in ascending order by given limits
class QNode :
    def __init__(self, data) :
        self.data = data
        self.next = None
    

#  Define custom queue class
class MyQueue :
    def __init__(self) :
        self.front = None
        self.rear = None
        self.size = 0
    
    #  Add a new node at last of queue
    def enqueue(self, data) :
        node = QNode(data)
        if (self.front == None) :
            #  When first node of queue
            self.front = node
        else :
            #  Add node at last level
            self.rear.next = node
        
        self.size += 1
        self.rear = node
    
    #  Delete front node of queue
    def dequeue(self) :
        if (self.front != None) :
            if (self.rear == self.front) :
                self.rear = None
                self.front = None
            else :
                self.front = self.front.next
            
            self.size -= 1
        
    
    def isSize(self) :
        return self.size
    
    def isEmpty(self) :
        if (self.isSize() == 0) :
            return True
        
        return False
    
    def peek(self) :
        if (self.isSize() == 0) :
            return None
        else :
            return self.front
        
    

class Jumping :
    def JumpingSteps(self, num) :
        if (num < 0) :
            return
        
        v = 0
        if (num <= 10) :
            while (v <= num) :
                print("  ", v, end = "")
                v += 1
            
            return
        
        record = MyQueue()
        count = 1
        digit = 0
        print("  0", end = "")
        while (count <= num or record.isEmpty() == False) :
            if (count <= num) :
                if (count <= 9) :
                    print("  ", count, end = "")
                    record.enqueue(count)
                    count += 1
                else :
                    #  Get head value
                    v = record.peek().data
                    #  Remove head node
                    record.dequeue()
                    #  Find last digit
                    digit = v % 10
                    if (digit != 0) :
                        #  Make new number
                        count = (v * 10) + (digit - 1)
                        if (count <= num) :
                            print("  ", count, end = "")
                            record.enqueue(count)
                        
                    
                    if (digit != 9 and count <= num) :
                        #  Make new number
                        count = (v * 10) + (digit + 1)
                        if (count <= num) :
                            print("  ", count, end = "")
                            record.enqueue(count)
                        
                    
                
            else :
                record.dequeue()
            
        
    

def main() :
    task = Jumping()
    #  0..1400
    task.JumpingSteps(1400)

if __name__ == "__main__": main()

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   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   878   898   987   989   1010   1012   1210   1212   1232   1234
#    Ruby program
#    Print all jumping numbers in ascending order by given limits
class QNode 
    # Define the accessor and reader of class QNode
    attr_reader :data, :next
    attr_accessor :data, :next
    def initialize(data) 
        self.data = data
        self.next = nil
    end

end

#  Define custom queue class
class MyQueue 
    # Define the accessor and reader of class MyQueue
    attr_reader :front, :rear, :size
    attr_accessor :front, :rear, :size
    def initialize() 
        self.front = nil
        self.rear = nil
        self.size = 0
    end

    #  Add a new node at last of queue
    def enqueue(data) 
        node = QNode.new(data)
        if (self.front == nil) 
            #  When first node of queue
            self.front = node
        else
 
            #  Add node at last level
            self.rear.next = node
        end

        self.size += 1
        self.rear = node
    end

    #  Delete front node of queue
    def dequeue() 
        if (self.front != nil) 
            if (self.rear == self.front) 
                self.rear = nil
                self.front = nil
            else
 
                self.front = self.front.next
            end

            self.size -= 1
        end

    end

    def isSize() 
        return self.size
    end

    def isEmpty() 
        if (self.isSize() == 0) 
            return true
        end

        return false
    end

    def peek() 
        if (self.isSize() == 0) 
            return nil
        else
 
            return self.front
        end

    end

end

class Jumping 
    def JumpingSteps(num) 
        if (num < 0) 
            return
        end

        v = 0
        if (num <= 10) 
            while (v <= num) 
                print("  ", v)
                v += 1
            end

            return
        end

        record = MyQueue.new()
        count = 1
        digit = 0
        print("  0")
        while (count <= num || record.isEmpty() == false) 
            if (count <= num) 
                if (count <= 9) 
                    print("  ", count)
                    record.enqueue(count)
                    count += 1
                else
 
                    #  Get head value
                    v = record.peek().data
                    #  Remove head node
                    record.dequeue()
                    #  Find last digit
                    digit = v % 10
                    if (digit != 0) 
                        #  Make new number
                        count = (v * 10) + (digit - 1)
                        if (count <= num) 
                            print("  ", count)
                            record.enqueue(count)
                        end

                    end

                    if (digit != 9 && count <= num) 
                        #  Make new number
                        count = (v * 10) + (digit + 1)
                        if (count <= num) 
                            print("  ", count)
                            record.enqueue(count)
                        end

                    end

                end

            else
 
                record.dequeue()
            end

        end

    end

end

def main() 
    task = Jumping.new()
    #  0..1400
    task.JumpingSteps(1400)
end

main()

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  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  878  898  987  989  1010  1012  1210  1212  1232  1234
/*
    Scala program
    Print all jumping numbers in ascending order by given limits
*/
class QNode(var data: Int,
    var next: QNode)
{
    def this(data: Int)
    {
        this(data, null);
    }
}
// Define custom queue class
class MyQueue(var front: QNode,
    var rear: QNode,
        var size: Int)
{
    def this()
    {
        this(null, null, 0);
    }
    // Add a new node at last of queue
    def enqueue(data: Int): Unit = {
        var node: QNode = new QNode(data);
        if (this.front == null)
        {
            // When first node of queue
            this.front = node;
        }
        else
        {
            // Add node at last level
            this.rear.next = node;
        }
        this.size += 1;
        this.rear = node;
    }
    // Delete front node of queue
    def dequeue(): Unit = {
        if (this.front != null)
        {
            if (this.rear == this.front)
            {
                this.rear = null;
                this.front = null;
            }
            else
            {
                this.front = this.front.next;
            }
            this.size -= 1;
        }
    }
    def isSize(): Int = {
        return this.size;
    }
    def isEmpty(): Boolean = {
        if (this.isSize() == 0)
        {
            return true;
        }
        return false;
    }
    def peek(): QNode = {
        if (this.isSize() == 0)
        {
            return null;
        }
        else
        {
            return this.front;
        }
    }
}
class Jumping()
{
    def JumpingSteps(num: Int): Unit = {
        if (num < 0)
        {
            return;
        }
        var v: Int = 0;
        if (num <= 10)
        {
            while (v <= num)
            {
                print("  " + v);
                v += 1;
            }
            return;
        }
        var record: MyQueue = new MyQueue();
        var count: Int = 1;
        var digit: Int = 0;
        print("  0");
        while (count <= num || record.isEmpty() == false)
        {
            if (count <= num)
            {
                if (count <= 9)
                {
                    print("  " + count);
                    record.enqueue(count);
                    count += 1;
                }
                else
                {
                    // Get head value
                    v = record.peek().data;
                    // Remove head node
                    record.dequeue();
                    // Find last digit
                    digit = v % 10;
                    if (digit != 0)
                    {
                        // Make new number
                        count = (v * 10) + (digit - 1);
                        if (count <= num)
                        {
                            print("  " + count);
                            record.enqueue(count);
                        }
                    }
                    if (digit != 9 && count <= num)
                    {
                        // Make new number
                        count = (v * 10) + (digit + 1);
                        if (count <= num)
                        {
                            print("  " + count);
                            record.enqueue(count);
                        }
                    }
                }
            }
            else
            {
                record.dequeue();
            }
        }
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var task: Jumping = new Jumping();
        // 0..1400
        task.JumpingSteps(1400);
    }
}

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  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  878  898  987  989  1010  1012  1210  1212  1232  1234
/*
    Swift 4 program
    Print all jumping numbers in ascending order by given limits
*/
class QNode
{
    var data: Int;
    var next: QNode? ;
    init(_ data: Int)
    {
        self.data = data;
        self.next = nil;
    }
}
// Define custom queue class
class MyQueue
{
    var front: QNode? ;
    var rear: QNode? ;
    var size: Int;
    init()
    {
        self.front = nil;
        self.rear = nil;
        self.size = 0;
    }
    // Add a new node at last of queue
    func enqueue(_ data: Int)
    {
        let node: QNode = QNode(data);
        if (self.front == nil)
        {
            // When first node of queue
            self.front = node;
        }
        else
        {
            // Add node at last level
            self.rear!.next = node;
        }
        self.size += 1;
        self.rear = node;
    }
    // Delete front node of queue
    func dequeue()
    {
        if (self.front  != nil)
        {
            if (self.rear === self.front)
            {
                self.rear = nil;
                self.front = nil;
            }
            else
            {
                self.front = self.front!.next;
            }
            self.size -= 1;
        }
    }
    func isSize() -> Int
    {
        return self.size;
    }
    func isEmpty() -> Bool
    {
        if (self.isSize() == 0)
        {
            return true;
        }
        return false;
    }
    func peek() -> QNode?
    {
        if (self.isSize() == 0)
        {
            return nil;
        }
        else
        {
            return self.front;
        }
    }
}
class Jumping
{
    func JumpingSteps(_ num: Int)
    {
        if (num < 0)
        {
            return;
        }
        var v: Int = 0;
        if (num <= 10)
        {
            while (v <= num)
            {
                print("  ", v, terminator: "");
                v += 1;
            }
            return;
        }
        let record: MyQueue = MyQueue();
        var count: Int = 1;
        var digit: Int = 0;
        print("  0", terminator: "");
        while (count <= num || record.isEmpty() == false)
        {
            if (count <= num)
            {
                if (count <= 9)
                {
                    print("  ", count, terminator: "");
                    record.enqueue(count);
                    count += 1;
                }
                else
                {
                    // Get head value
                    v = record.peek()!.data;
                    // Remove head node
                    record.dequeue();
                    // Find last digit
                    digit = v % 10;
                    if (digit  != 0)
                    {
                        // Make new number
                        count = (v * 10) + (digit - 1);
                        if (count <= num)
                        {
                            print("  ", count, terminator: "");
                            record.enqueue(count);
                        }
                    }
                    if (digit  != 9 && count <= num)
                    {
                        // Make new number
                        count = (v * 10) + (digit + 1);
                        if (count <= num)
                        {
                            print("  ", count, terminator: "");
                            record.enqueue(count);
                        }
                    }
                }
            }
            else
            {
                record.dequeue();
            }
        }
    }
}
func main()
{
    let task: Jumping = Jumping();
    // 0..1400
    task.JumpingSteps(1400);
}
main();

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   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   878   898   987   989   1010   1012   1210   1212   1232   1234
/*
    Kotlin program
    Print all jumping numbers in ascending order by given limits
*/
class QNode
{
    var data: Int;
    var next: QNode ? ;
    constructor(data: Int)
    {
        this.data = data;
        this.next = null;
    }
}
// Define custom queue class
class MyQueue
{
    var front: QNode ? ;
    var rear: QNode ? ;
    var size: Int;
    constructor()
    {
        this.front = null;
        this.rear = null;
        this.size = 0;
    }
    // Add a new node at last of queue
    fun enqueue(data: Int): Unit
    {
        val node: QNode = QNode(data);
        if (this.front == null)
        {
            // When first node of queue
            this.front = node;
        }
        else
        {
            // Add node at last level
            this.rear?.next = node;
        }
        this.size += 1;
        this.rear = node;
    }
    // Delete front node of queue
    fun dequeue(): Unit
    {
        if (this.front != null)
        {
            if (this.rear == this.front)
            {
                this.rear = null;
                this.front = null;
            }
            else
            {
                this.front = this.front?.next;
            }
            this.size -= 1;
        }
    }
    fun isSize(): Int
    {
        return this.size;
    }
    fun isEmpty(): Boolean
    {
        if (this.isSize() == 0)
        {
            return true;
        }
        return false;
    }
    fun peek(): QNode ?
    {
        if (this.isSize() == 0)
        {
            return null;
        }
        else
        {
            return this.front;
        }
    }
}
class Jumping
{
    fun JumpingSteps(num: Int): Unit
    {
        if (num < 0)
        {
            return;
        }
        var v: Int = 0;
        if (num <= 10)
        {
            while (v <= num)
            {
                print("  " + v);
                v += 1;
            }
            return;
        }
        val record: MyQueue = MyQueue();
        var count: Int = 1;
        var digit: Int ;
        print("  0");
        while (count <= num || record.isEmpty() == false)
        {
            if (count <= num)
            {
                if (count <= 9)
                {
                    print("  " + count);
                    record.enqueue(count);
                    count += 1;
                }
                else
                {
                    // Get head value
                    v = record.peek()!!.data;
                    // Remove head node
                    record.dequeue();
                    // Find last digit
                    digit = v % 10;
                    if (digit != 0)
                    {
                        // Make new number
                        count = (v * 10) + (digit - 1);
                        if (count <= num)
                        {
                            print("  " + count);
                            record.enqueue(count);
                        }
                    }
                    if (digit != 9 && count <= num)
                    {
                        // Make new number
                        count = (v * 10) + (digit + 1);
                        if (count <= num)
                        {
                            print("  " + count);
                            record.enqueue(count);
                        }
                    }
                }
            }
            else
            {
                record.dequeue();
            }
        }
    }
}
fun main(args: Array < String > ): Unit
{
    val task: Jumping = Jumping();
    // 0..1400
    task.JumpingSteps(1400);
}

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  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  878  898  987  989  1010  1012  1210  1212  1232  1234

Time Complexity

The time complexity of generating and printing jumping numbers is O(n), where n is the number of jumping numbers within the given limit. The number of jumping numbers is relatively smaller than the given limit, so the process is efficient.

Resultant Output Explanation

The output displays the jumping numbers in ascending order within the specified limit. The example output shows the jumping numbers from 0 to 1400, confirming the correctness of the algorithm. Each jumping number is separated by spaces for clarity. This demonstrates how the algorithm efficiently generates and displays jumping numbers based on the adjacent digit difference property.

Comment

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