Print Nth Stepping or Autobiographical number
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++ implementation to find // N’th stepping natural Number #include <bits/stdc++.h> using namespace std; // Function to find the // Nth stepping natural number int NthSmallest(int K) { // Declare the queue queue<int> Q; int x; // Enqueue 1, 2, ..., 9 in this order for (int i = 1; i < 10; i++) Q.push(i); // Perform K operation on queue for (int i = 1; i <= K; i++) { // Get the ith Stepping number x = Q.front(); // Perform Dequeue from the Queue Q.pop(); // If x mod 10 is not equal to 0 if (x % 10 != 0) { // then Enqueue 10x + (x mod 10) - 1 Q.push(x * 10 + x % 10 - 1); } // Enqueue 10x + (x mod 10) Q.push(x * 10 + x % 10); // If x mod 10 is not equal to 9 if (x % 10 != 9) { // then Enqueue 10x + (x mod 10) + 1 Q.push(x * 10 + x % 10 + 1); } } // Return the dequeued number of the K-th // operation as the Nth stepping number return x; } // Driver Code int main() { // initialise K int N = 16; cout << NthSmallest(N) << "\n"; return 0; } |
32
Time Complexity: O(N)
Recommended Posts:
- Find all Autobiographical Numbers with given number of digits
- Number of n digit stepping numbers
- Number of n digit stepping numbers | Space optimized solution
- Print a number strictly less than a given number such that all its digits are distinct.
- Print Bracket Number
- Queries to print all divisors of number
- C Program to Print all digits of a given number
- Print Number series without using any loop
- Print all Prime Quadruplet of a number less than it
- Print all substring of a number without any conversion
- Print steps to make a number in form of 2^X - 1
- Program to print factors of a number in pairs
- Program to print multiplication table of a number
- Print all combinations of points that can compose a given number
- Program to print 'N' alphabet using the number pattern from 1 to n
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.

