Program to print first N Stepping numbers
Last Updated :
24 Mar, 2023
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:
- Generate all possible Stepping numbers till 1000, for easy computation
- For each value of N, just print the N already computed Stepping numbers
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void generateSteppingNos(
int x, set< int >& s)
{
if (x > 1e8)
return ;
s.insert(x);
int last = x % 10;
if (last - 1 >= 0)
generateSteppingNos(
x * 10 + last - 1,
s);
generateSteppingNos(
x * 10 + last,
s);
if (last + 1 <= 9)
generateSteppingNos(
x * 10 + last + 1,
s);
}
void NSteppingNumbers( int N)
{
set< int > s;
for ( int i = 1; i <= 9; i++)
generateSteppingNos(i, s);
int count = 1;
for ( auto & it : s) {
if (count <= N) {
cout << it << ", " ;
count++;
}
else
break ;
}
}
int main()
{
int N = 14;
NSteppingNumbers(N);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static HashSet<Integer> s = new HashSet<Integer>();
static void generateSteppingNos( int x)
{
if (x > 1e8)
return ;
s.add(x);
int last = x % 10 ;
if (last - 1 >= 0 )
generateSteppingNos(x * 10 + last - 1 );
generateSteppingNos(x * 10 + last);
if (last + 1 <= 9 )
generateSteppingNos(x * 10 + last + 1 );
}
static void NSteppingNumbers( int N)
{
HashSet<Integer> s = new HashSet<Integer>();
int [] arr = { 10 , 11 , 12 , 21 , 22 };
for ( int i = 1 ; i <= 9 ; i++)
{
generateSteppingNos(i);
s.add(i);
}
for ( int i = 0 ; i < arr.length; i++)
{
s.add(arr[i]);
}
int count = 1 ;
for ( int it : s)
{
if (count <= N) {
System.out.print(it + ", " );
count++;
}
else
break ;
}
}
public static void main(String[] args) {
int N = 14 ;
NSteppingNumbers(N);
}
}
|
Python3
def generateSteppingNos(x, s):
if (x > 1e8 ):
return
s.add(x)
last = x % 10
if (last - 1 > = 0 ):
generateSteppingNos(x * 10 + last - 1 , s)
generateSteppingNos(x * 10 + last, s)
if (last + 1 < = 9 ):
generateSteppingNos(x * 10 + last + 1 , s)
def NSteppingNumbers(N):
s = set ()
for i in range ( 1 , 10 ):
generateSteppingNos(i, s)
count = 1
s.pop()
for value in s:
if (count < = N):
print (value, end = ', ' )
count = count + 1
else :
break
N = 14
NSteppingNumbers(N)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static HashSet< int > s = new HashSet< int >();
static void generateSteppingNos( int x)
{
if (x > 1e8)
return ;
s.Add(x);
int last = x % 10;
if (last - 1 >= 0)
generateSteppingNos(x * 10 + last - 1);
generateSteppingNos(x * 10 + last);
if (last + 1 <= 9)
generateSteppingNos(x * 10 + last + 1);
}
static void NSteppingNumbers( int N)
{
HashSet< int > s = new HashSet< int >();
int [] arr = {10, 11, 12, 21, 22};
for ( int i = 1; i <= 9; i++)
{
generateSteppingNos(i);
s.Add(i);
}
for ( int i = 0; i < arr.Length; i++)
{
s.Add(arr[i]);
}
int count = 1;
foreach ( int it in s)
{
if (count <= N) {
Console.Write(it + ", " );
count++;
}
else
break ;
}
}
static void Main() {
int N = 14;
NSteppingNumbers(N);
}
}
|
Javascript
let s = new Set();
function generateSteppingNos(x) {
if (x > 1e8) {
return ;
}
s.add(x);
let last = x % 10;
if (last - 1 >= 0) {
generateSteppingNos(x * 10 + last - 1);
}
generateSteppingNos(x * 10 + last);
if (last + 1 <= 9) {
generateSteppingNos(x * 10 + last + 1);
}
}
function NSteppingNumbers(N) {
let s = new Set();
let arr = [10, 11, 12, 21, 22];
for (let i = 1; i <= 9; i++) {
generateSteppingNos(i);
s.add(i);
}
for (let i = 0; i < arr.length; i++) {
s.add(arr[i]);
}
let count = 1;
let ans = [];
for (let it of s) {
if (count <= N) {
ans.push(it);
count++;
} else {
break ;
}
}
console.log(ans.join( ', ' ))
}
let N = 14;
NSteppingNumbers(N);
|
Output:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 21, 22,
Share your thoughts in the comments
Please Login to comment...