Print Nth Stepping or Autobiographical number
Last Updated :
21 Nov, 2021
Given a natural number N, the task is to print the Nth Stepping or Autobiographical number.
A number is called stepping number if all adjacent digits have an absolute difference of 1. The following series is a list of Stepping natural numbers:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 21, 22, 23, 32, ….
Examples:
Input: N = 16
Output: 32
Explanation:
16th Stepping number is 32.
Input: N = 14
Output: 22
Explanation:
14th Stepping number is 22.
Approach: This problem can be solved using Queue data structure. First, prepare an empty queue, and Enqueue 1, 2, …, 9 in this order.
Then inorder the generate the Nth Stepping number, the following operations has to be performed N times:
- Perform Dequeue from the Queue. Let x be the dequeued element.
- If x mod 10 is not equal to 0, then Enqueue 10x + (x mod 10) – 1
- Enqueue 10x + (x mod 10).
- If x mod 10 is not equal to 9, then Enqueue 10x + (x mod 10) + 1.
The dequeued number in the N-th operation is the N-th Stepping Number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int NthSmallest( int K)
{
queue< int > Q;
int x;
for ( int i = 1; i < 10; i++)
Q.push(i);
for ( int i = 1; i <= K; i++) {
x = Q.front();
Q.pop();
if (x % 10 != 0) {
Q.push(x * 10 + x % 10 - 1);
}
Q.push(x * 10 + x % 10);
if (x % 10 != 9) {
Q.push(x * 10 + x % 10 + 1);
}
}
return x;
}
int main()
{
int N = 16;
cout << NthSmallest(N) << "\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int NthSmallest( int K)
{
Queue<Integer> Q = new LinkedList<>();
int x = 0 ;
for ( int i = 1 ; i < 10 ; i++)
Q.add(i);
for ( int i = 1 ; i <= K; i++) {
x = Q.peek();
Q.remove();
if (x % 10 != 0 ) {
Q.add(x * 10 + x % 10 - 1 );
}
Q.add(x * 10 + x % 10 );
if (x % 10 != 9 ) {
Q.add(x * 10 + x % 10 + 1 );
}
}
return x;
}
public static void main(String[] args)
{
int N = 16 ;
System.out.print(NthSmallest(N));
}
}
|
Python3
def NthSmallest(K):
Q = []
for i in range ( 1 , 10 ):
Q.append(i)
for i in range ( 1 ,K + 1 ):
x = Q[ 0 ]
Q.remove(Q[ 0 ])
if (x % 10 ! = 0 ):
Q.append(x * 10 + x % 10 - 1 )
Q.append(x * 10 + x % 10 )
if (x % 10 ! = 9 ):
Q.append(x * 10 + x % 10 + 1 )
return x
if __name__ = = '__main__' :
N = 16
print (NthSmallest(N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int NthSmallest( int K)
{
List< int > Q = new List< int >();
int x = 0;
for ( int i = 1; i < 10; i++)
Q.Add(i);
for ( int i = 1; i <= K; i++) {
x = Q[0];
Q.RemoveAt(0);
if (x % 10 != 0) {
Q.Add(x * 10 + x % 10 - 1);
}
Q.Add(x * 10 + x % 10);
if (x % 10 != 9) {
Q.Add(x * 10 + x % 10 + 1);
}
}
return x;
}
public static void Main(String[] args)
{
int N = 16;
Console.Write(NthSmallest(N));
}
}
|
Javascript
<script>
function NthSmallest(K)
{
var Q = [];
var x;
for ( var i = 1; i < 10; i++)
Q.push(i);
for ( var i = 1; i <= K; i++) {
x = Q[0];
Q.shift();
if (x % 10 != 0) {
Q.push(x * 10 + x % 10 - 1);
}
Q.push(x * 10 + x % 10);
if (x % 10 != 9) {
Q.push(x * 10 + x % 10 + 1);
}
}
return x;
}
var N = 16;
document.write( NthSmallest(N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...