Given a positive integer **N**, the task is to find the **N ^{th}** natural number after removing all the natural numbers containing digit

**9**.

**Examples:**

Input:N = 8Output:8Explanation:

Since 9 is the first natural number that contains the digit 9 and is the 9th natural number, therefore, no removal required to find the 8^{th }natural number, which is 8.

Input:N = 9Output:10Explanation:

Removing number 9, the first 9 natural numbers are {1, 2, 3, 4, 5, 6, 7, 8, 10}.

Therefore, the 9^{th}natural number is 10.

**Naive Approach: **The simplest approach to solve the above problem is to iterate up to **N** and keep excluding all numbers less than **N **containing the digit **9.** Finally, print the **N**^{th} natural number obtained.**Time Complexity: **O(N)**Auxiliary Space:** O(1)

**Efficient Approach: **The above approach can be optimized based on the following observations:

- It is known that, digits of base
2numbers varies from0to1. Similarly, digits of base10numbers varies from0to9.- Therefore, the digits of base
9numbers will vary from0to8.- It can be observed that
Nnumber in base^{th}9is equal toNnumber after skipping numbers containing digit^{th}9.- So the task is reduced to find the base
9equivalent of the numberN.

Follow the steps below to solve the problem:

- Initialize two variables, say
**res = 0**and**p = 1**,**9**and to store the position of a digit. - Iterate while
**N**is greater than**0**and perform the following operations:- Update
**res**as**res = res + p*(N%9)**. - Divide
**N**by**9**and multiply**p**by**10.**

- Update
- After completing the above steps, print the value of
**res**.

Below is the implementation of the above approach:

## C++

`// C++ implementataion of above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find Nth number in base 9` `long` `long` `findNthNumber(` `long` `long` `N)` `{` ` ` `// Stores the Nth number` ` ` `long` `long` `result = 0;` ` ` `long` `long` `p = 1;` ` ` `// Iterate while N is` ` ` `// greater than 0` ` ` `while` `(N > 0) {` ` ` `// Update result` ` ` `result += (p * (N % 9));` ` ` `// Divide N by 9` ` ` `N = N / 9;` ` ` `// Multiply p by 10` ` ` `p = p * 10;` ` ` `}` ` ` `// Return result` ` ` `return` `result;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 9;` ` ` `cout << findNthNumber(N);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.util.*;` `class` `GFG` `{` ` ` `// Function to find Nth number in base 9` ` ` `static` `long` `findNthNumber(` `long` `N)` ` ` `{` ` ` `// Stores the Nth number` ` ` `long` `result = ` `0` `;` ` ` `long` `p = ` `1` `;` ` ` `// Iterate while N is` ` ` `// greater than 0` ` ` `while` `(N > ` `0` `) {` ` ` `// Update result` ` ` `result += (p * (N % ` `9` `));` ` ` `// Divide N by 9` ` ` `N = N / ` `9` `;` ` ` `// Multiply p by 10` ` ` `p = p * ` `10` `;` ` ` `}` ` ` `// Return result` ` ` `return` `result;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `N = ` `9` `;` ` ` `System.out.print(findNthNumber(N));` ` ` `}` `}` `// This code is contributed by splevel62.` |

## Python3

`# Python 3 implementataion of above approach` `# Function to find Nth number in base 9` `def` `findNthNumber(N):` ` ` ` ` `# Stores the Nth number` ` ` `result ` `=` `0` ` ` `p ` `=` `1` ` ` `# Iterate while N is` ` ` `# greater than 0` ` ` `while` `(N > ` `0` `):` ` ` ` ` `# Update result` ` ` `result ` `+` `=` `(p ` `*` `(N ` `%` `9` `))` ` ` `# Divide N by 9` ` ` `N ` `=` `N ` `/` `/` `9` ` ` `# Multiply p by 10` ` ` `p ` `=` `p ` `*` `10` ` ` `# Return result` ` ` `return` `result` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `N ` `=` `9` ` ` `print` `(findNthNumber(N))` ` ` ` ` `# This code is contributed by bgangwar59.` |

## C#

`// C# implementataion of above approach` `using` `System;` `class` `GFG` `{` ` ` `// Function to find Nth number in base 9` ` ` `static` `long` `findNthNumber(` `long` `N)` ` ` `{` ` ` `// Stores the Nth number` ` ` `long` `result = 0;` ` ` `long` `p = 1;` ` ` `// Iterate while N is` ` ` `// greater than 0` ` ` `while` `(N > 0) {` ` ` `// Update result` ` ` `result += (p * (N % 9));` ` ` `// Divide N by 9` ` ` `N = N / 9;` ` ` `// Multiply p by 10` ` ` `p = p * 10;` ` ` `}` ` ` ` ` `// Return result` ` ` `return` `result;` ` ` `}` ` ` `// Driver code ` ` ` `static` `void` `Main ()` ` ` `{` ` ` `int` `N = 9;` ` ` `Console.Write(findNthNumber(N));` ` ` `}` `}` `// This code is contributed by divyesh072019.` |

**Output:**

10

**Time Complexity: **O(log_{9} N)**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.