Print Nth Stepping or Autobiographical number


In this problem, we will print the Nth Stepping number.

The naïve approach for solving the problem is to traverse natural numbers, check whether each number is a Stepping number, and find the Nth Stepping number. Another approach can be using the queue data structure.

Problem Statement

We have given a positive integer N. We need to print the Nth Stepping number. The number is called a Stepping number if the difference between two adjacent digits of a number is 1.

Sample Examples

Input

N = 15

Output

34

Explanation

The Stepping numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 21, 23, 32, 34. So, the 15th Stepping number is 34.

Input

N = 2

Output

2

Explanation

1 to 10 digits are Stepping numbers.

Approach 1

In this approach, we will traverse the natural numbers until we find the Nth Stepping number. To find the Stepping number, we will check the difference of each adjacent digit.

Algorithm

  • Step 1 − Initialize the p with 1, and make traversal using the loop while n is greater than 0.

  • Step 2 − In the loop, call the checkForStepping() function by passing p as a parameter to check whether the p is a stepping number.

  • Step 2.1 − In the checkForStepping() function, store the value of the num % 10 into the p_digit representing the previous digit. Also, divide the num by 10.

  • Step 2.2 − Make traversal while num is greater than 0.

  • Step 2.3.1 − Store the number's last digit in the c_digit variable.

  • Step 2.3.2 − Return false if the absolute difference between the c_digit and p_digit is not 1.

  • Step 2.3.3 − Update the p_digit with the value of the c_digit and divide the num by 10.

  • Step 3 − If the number is a Stepping number, decrement value of n by 1.

  • Step 4 − Increment the value of p by 1.

  • Step 5 − Return p – 1, as in the last iteration, p is increased by 1.

Example

#include <bits/stdc++.h>
using namespace std;

bool checkForStepping(int num) {
   int p_digit = num % 10;
   num /= 10;
   while (num) {
      // Get the current digit
      int c_digit = num % 10;
      // Check the difference between adjacent digits
      if (abs(p_digit - c_digit) != 1)
         return false;
      p_digit = c_digit;
      num /= 10;
   }
   return true;
}
int NthSteppingNumber(int n) {
   int p = 1;
   // Finding N stepping numbers
   while (n > 0) {
      if (checkForStepping(p)) {
         n--;
      }
      p++;
   }
   return p - 1;
}
int main() {
   int N = 15;
   cout << "The Nth Stepping or Autobiographical number is " << NthSteppingNumber(N) << "\n";
   return 0;
}

Output

The Nth Stepping or Autobiographical number is 34
  • Time complexity − O(P), Where P is the total natural numbers that we need to traverse.

  • Space complexity − O(1)

Approach 2

In this approach, we will use the queue data structure to store the previous Stepping number. After that, we will find the next Stepping numbers by performing the multiplication, addition, and modulo operation on the previous stepping number.

Algorithm

  • Step 1 − Define the queue to store the previous numbers. Also, define the temp integer variable.

  • Step 2 − Insert 1 to 10 numbers into the queue, as they are Stepping numbers.

  • Step 3 − Now, Make N traversals using the loop.

  • Step 3.1 − Pop the first element from the queue.

  • Step 3.2 − If the number is not divisible by 10, insert the temp * 10 + temp % 10 − 1 into the queue. It generates a new Stepping number from the current number. For example, if the current number is 2, it generates 21.

  • Step 3.3 − If temp % 10 is not equal to 9, insert the temp * 10 + temp % 10 + 1 into the queue. It generates the 23 from 2.

  • Step 4 − Return temp value.

Example

#include <bits/stdc++.h>
using namespace std;

int NthSteppingNumber(int K) {
   queue<int> que;
   int temp;
   // Insert 1 to 9
   for (int p = 1; p < 10; p++)
      que.push(p);
   for (int p = 1; p <= K; p++) {
      temp = que.front();
      que.pop();
      // When the number is not divisible by 10
      if (temp % 10 != 0) {
         que.push(temp * 10 + temp % 10 - 1);
      }
      // When the number doesn't contain 9 as a last digit
      if (temp % 10 != 9) {
         que.push(temp * 10 + temp % 10 + 1);
      }
   }
   return temp;
}
int main() {
   int N = 15;
   cout << "The Nth Stepping or Autobiographical number is " << NthSteppingNumber(N) << "\n";
   return 0;
}

Output

The Nth Stepping or Autobiographical number is 34
  • Time complexity − O(N)

  • Space complexity − O(N) to store numbers in the queue.

Conclusion

In the first approach, we are required to make a P traversal, but in the second approach, we make traversals equal to N. When we need to find a large Stepping number like the 10000th Stepping number, the first approach can be more time expensive.

Updated on: 25-Aug-2023

117 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements