Skip to content
Related Articles
Open in App
Not now

Related Articles

Nth natural number after removing all numbers consisting of the digit 9

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 28 Nov, 2022
Improve Article
Save Article

Given a positive integer N, the task is to find the Nth natural number after removing all the natural numbers containing digit 9.

Examples:

Input: N = 8
Output: 8
Explanation:
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 8th natural number, which is 8.

Input: N = 9
Output: 10
Explanation:
Removing number 9, the first 9 natural numbers are {1, 2, 3, 4, 5, 6, 7, 8, 10}.
Therefore, the 9th natural number is 10.

Recommended Practice

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 Nth natural number obtained.

Follow below steps below to solve the problems : 

  • Initialize  one variable count = 0
  • and use for loop and pass the element of the loop to isDigitNine(i) function to check whether that number contains 9 or not and increment that if not present 
  • And once count hits N assign the last i to count and break the loop.
  • Return answer as count 

C++




#include <bits/stdc++.h>
using namespace std;
 
// isDigitNine function return true if number contain digit
// 9 else will return false
bool isDigitNine(int i)
{
    while (i > 0) {
        int rem = i % 10;
        if (rem == 9) {
            return true;
        }
        i = i / 10;
    }
    return false;
}
 
long long findNth(long long N)
{
    long long count = 0;
    for (int i = 1; i > 0; i++) {
        // call function digitnine() with i
        if (isDigitNine(i) == false) {
            count++;
            if (count == N) {
                count = i; // once  count is equal to N then
                break; // assign last i to count and break
                       // the loop
            }
        }
    }
    return count;
}
 
int main()
{
    long long N = 18976;
    long long ans = findNth(N);
    cout << ans << endl;
    return 0;
}

Java




public class nthnaturalNum {
    public static void main(String[] args) {
        long N = 18976;
        long ans = findNth(N);
        System.out.println(ans);
         
    }
    static long findNth(long N)
    {
        //code here
        long count = 0;
        for(int i = 1; i > 0; i++ ){
          // call function digitnine() with i
            if(isDigitNine(i) == false)
            {
                count ++;
                if(count == N ) {
                    count = i; // once  count is equal to N then
                     break;    //assign last i to count and break the loop
                }
            }
             
             
        }
        return count ;
    }
    // isDigitNine function return true if number contain digit 9
    // else will return false
     static boolean isDigitNine(int i){
        while(i > 0){
            int rem = i % 10;
            if(rem == 9){
                return true;
            }
            i = i / 10;
        }
        return false;
    }
     
}
/* This code is contributed by devendra solunke */

Python3




# defining findNth function to find the Nth value
def findNth(N):
    count = 0
    i = 1
    while(i != 0):
       
        # calling isDigitNine to check the digit is nine or not
        if(isDigitNine(i) == False):
            count = count + 1
            if(count == N):
                count = i # break when we reached to the count equals to N
                break
        i = i + 1
    return count
 
# defining isDigitNine function to check digit is 9 or nor
def isDigitNine(i):
    while(i != 0):
        rem = i % 10
        if(rem == 9):
            return True
        i = i//10
         
    return False
 
N = 18976
solution = findNth(N)
print(solution)
 
# This code is contributed by Gayatri Deshmukh

C#




using System;
 
public class gfg {
    static bool isDigitNine(int i)
    {
        while (i > 0) {
            int rem = i % 10;
            if (rem == 9) {
                return true;
            }
            i = i / 10;
        }
        return false;
    }
 
    static long findNth(long N)
    {
        long count = 0;
        for (int i = 1; i > 0; i++)
        {
           
            // call function digitnine() with i
            if (isDigitNine(i) == false) {
                count++;
                if (count == N) {
                    count = i; // once  count is equal to N
                               // then
                    break; // assign last i to count and
                           // break the loop
                }
            }
        }
        return count;
    }
    public static void Main(string[] args)
    {
        long N = 18976;
        long ans = findNth(N);
        Console.WriteLine(ans);
    }
}
 
// This code is contributed by ishankhanelwals.

Javascript




// isDigitNine function return true if number contain digit
// 9 else will return false
function isDigitNine(i)
{
    while (i > 0) {
        let rem = i % 10;
        if (rem == 9) {
            return true;
        }
        i =Math.floor( i / 10);
    }
    return false;
}
 
function findNth(N)
{
    let count = 0;
    for (let i = 1; i > 0; i++) {
        // call function digitnine() with i
        if (isDigitNine(i) == false) {
            count++;
            if (count == N) {
                count = i; // once  count is equal to N then
                break; // assign last i to count and break
                       // the loop
            }
        }
    }
    return count;
}
 
// Driver code
    let N = 18976;
    let ans = findNth(N);
    console.log(ans);
     
// This code is contributed by ishankhandelwals.

Output

28024

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 2 numbers varies from 0 to 1. Similarly, digits of base 10 numbers varies from 0 to 9.
  • Therefore, the digits of base 9 numbers will vary from 0 to 8.
  • It can be observed that Nth number in base 9 is equal to Nth number after skipping numbers containing digit 9.
  • So the task is reduced to find the base 9 equivalent of the number N.

Follow the steps below to solve the problem:

  • Initialize two variables, say res = 0 and p = 1, to store the number in base 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.
  • After completing the above steps, print the value of res.

Below is the implementation of the above approach:

C++




// C++ implementation 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 implementation 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# implementation 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.

Javascript




<script>
 
    // Javascript implementation of above approach
     
    // Function to find Nth number in base 9
    function findNthNumber(N)
    {
      // Stores the Nth number
      let result = 0;
 
      let p = 1;
 
      // Iterate while N is
      // greater than 0
      while (N > 0) {
 
        // Update result
        result += (p * (N % 9));
 
        // Divide N by 9
        N = parseInt(N / 9, 10);
 
        // Multiply p by 10
        p = p * 10;
      }
 
      // Return result
      return result;
    }
     
    let N = 9;
    document.write(findNthNumber(N));
   
</script>

Output

10

Time Complexity: O(log9 N)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!