Program to print first N Stepping numbers

Given a number N, the task is to print the first N Stepping Numbers.

A number is called stepping number if all adjacent digits have an absolute difference of 1. For e.g. 321 is a Stepping Number while 421 is not.

Examples:



Input: N = 7
Output: 1, 2, 3, 4, 5, 6, 7

Input: N = 14
Output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 21, 22

Naive approach: We can start from 1 and check for every number whether they are Stepping number or not and continue till we find the K-th Stepping number.

Efficient Approach:

  1. Generate all possible Stepping numbers till 1000, for easy computation
  2. For each value of N, just print the N already computed Stepping numbers

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to print first N
// Stepping numbers
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to generate
// the Stepping numbers
void generateSteppingNos(
    int x, set<int>& s)
{
    if (x > 1e8)
        return;
  
    // Inserting the current
    // element in the set
    s.insert(x);
  
    // Retrieving the last digit
    // of the current number
    int last = x % 10;
  
    if (last - 1 >= 0)
  
        // Appending x-1 to
        // the current number
        generateSteppingNos(
            x * 10 + last - 1,
            s);
  
    // Appending x to
    // the current number
    generateSteppingNos(
        x * 10 + last,
        s);
  
    if (last + 1 <= 9)
  
        // Appending x+1 to
        // the current number
        generateSteppingNos(
            x * 10 + last + 1,
            s);
}
  
// Function to print
// N Stepping numbers
void NSteppingNumbers(int N)
{
    set<int> s;
  
    for (int i = 1; i <= 9; i++)
        generateSteppingNos(i, s);
  
    int count = 1;
  
    // Printing N numbers from s
    for (auto& it : s) {
        if (count <= N) {
            cout << it << ", ";
            count++;
        }
        else
            break;
    }
}
  
// Driver code
int main()
{
    int N = 14;
  
    NSteppingNumbers(N);
  
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to print first N 
# Stepping numbers 
  
# Function to generate 
# the Stepping numbers 
def generateSteppingNos(x, s): 
    if (x > 1e8):
        return
  
    # Inserting the current 
    # element in the set 
    s.add(x)
  
    # Retrieving the last digit 
    # of the current number 
    last = x % 10
  
    if (last - 1 >= 0):
  
        # Appending x-1 to 
        # the current number 
        generateSteppingNos(x * 10 + last - 1, s)
  
    # Appending x to 
    # the current number 
    generateSteppingNos(x * 10 + last, s) 
  
    if (last + 1 <= 9):
      
        # Appending x+1 to 
        # the current number 
        generateSteppingNos(x * 10 + last + 1, s)
  
# Function to print 
# N Stepping numbers 
def NSteppingNumbers(N):
  
    s = set()
  
    for i in range(1, 10): 
        generateSteppingNos(i, s)
  
    count = 1
  
    s.pop()
  
    # Printing N numbers from s 
    for value in s: 
        if (count <= N): 
            print(value, end=', ')
            count = count + 1
        else:
            break
  
# Driver code 
N = 14
  
NSteppingNumbers(N)
  
# This code is contributed by Sanjit_Prasad

chevron_right


Output:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 21, 22,

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Sanjit_Prasad