Skip to content
Related Articles

Related Articles

Minimum number to be added to N so that it does not contains digit D

Improve Article
Save Article
Like Article
  • Last Updated : 13 Dec, 2021

Given a positive integer N and a digit D, the task is to find the smallest non-negative number required to be added to the given number such that after addition, the resultant number should not contain the digit D.

Example:

Input: N = 25, D = 2
Output: 5
Explanation: The number 30 is the smallest number after 25 which does not contain digit 2. So the required number must be added to 25 to remove digit 2 is 30 – 25  = 5 

Input: N = 100, D = 0
Output: 11
Explanation: The number 111 is the smallest number after 100 which does not have digit 0. So the required number must be added to 100 to remove digit 0 is 111 – 100  = 11 

 

Approach: This problem can be solved by iterating over the digits of the given number from right to left and finding the nearest digit that does not contain digit D.

Follow the steps mentioned below:

  • Iterate the given number and check if the given digit is present in the number or not.
  • If the current digit matches the given digit then check for the following conditions:
    • If the current digit is in the range 1 to 9 then, Increment the current digit by 1 and make all digits to its right as 0.
    • Else if, the current digit is 0 then, Increment the current digit by 1 and make all digits to it right as 1
  • The difference between the number formed using the above condition and the original number is the required smallest number.

Below is the implementation of the above approach:

C++




// C++ implementation of the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate smallest positive
// number to be added to remove
// the given digit
int smallestNumber(int n, int d)
{
    // Copy the number to another variable
    int temp = n;
 
    int ans = 0, count = 0;
 
    // Iterate over digits of the given
    // number from right to left
    while (temp > 0) {
 
        // Extract the last digit of the
        // number and check if it is
        // equal to the given digit (d)
        int remainder = temp % 10;
        temp = temp / 10;
        count++;
 
        if (remainder == d) {
            // If the digit which
            // need to be replaced
            // is equal to 0 then
            // increment the current
            // digit and make the all digits
            // to its right 1
            if (d == 0) {
 
                string num = string(count, '1');
            
                temp = temp * pow(10, count) + stoi(num);
            }
 
            // Else, increment the current digit
            // by 1 and make the all digits
            // to its right 0
            else {
 
                temp = temp * pow(10, count)
                       + (remainder + 1)
                             * pow(10, count - 1);
            }
            // After the removal of given
            // digit the number will be temp
            // So, the required smallest
            // number is (temp - n)
            ans = temp - n;
 
            // Set count as 0
            count = 0;
        }
    }
 
    // Return the result
    return ans;
}
 
// Driver code
int main()
{
    int N = 100;
    int D = 0;
 
    // print the smallest number required
    // to be added to remove
    // the digit 'D' in number 'N'
    cout << smallestNumber(N, D) << "\n";
 
    return 0;
}

Java




// Java implementation of the above approach
class GFG{
 
// Function to calculate smallest positive
// number to be added to remove
// the given digit
public static int smallestNumber(int n, int d)
{
     
    // Copy the number to another variable
    int temp = n;
 
    int ans = 0, count = 0;
 
    // Iterate over digits of the given
    // number from right to left
    while (temp > 0)
    {
         
        // Extract the last digit of the
        // number and check if it is
        // equal to the given digit (d)
        int remainder = temp % 10;
        temp = temp / 10;
        count++;
 
        if (remainder == d)
        {
             
            // If the digit which need to be
            // replaced is equal to 0 then
            // increment the current digit
            // and make the all digits to
            // its right 1
            if (d == 0)
            {
                String num = "";
                for(int i = 0; i < count; i++)
                {
                    num = num + "1";
                }
                temp = (int)(temp * Math.pow(10, count) +
                       Integer.parseInt(num));
            }
 
            // Else, increment the current digit
            // by 1 and make the all digits
            // to its right 0
            else
            {
                temp = (int) (temp * Math.pow(10, count) +
                   (remainder + 1) * Math.pow(10, count - 1));
            }
             
            // After the removal of given
            // digit the number will be temp
            // So, the required smallest
            // number is (temp - n)
            ans = temp - n;
 
            // Set count as 0
            count = 0;
        }
    }
 
    // Return the result
    return ans;
}
 
// Driver code
public static void main(String args[])
{
    int N = 100;
    int D = 0;
 
    // Print the smallest number required
    // to be added to remove the digit 'D'
    // in number 'N'
    System.out.println(smallestNumber(N, D));
}
}
 
// This code is contributed by _saurabh_jaiswal

Python3




# Python 3 implementation of the above approach
 
# Function to calculate smallest positive
# number to be added to remove
# the given digit
def smallestNumber(n, d):
 
    # Copy the number to another variable
    temp = n
 
    ans = 0
    count = 0
 
    # Iterate over digits of the given
    # number from right to left
    while (temp > 0):
 
        # Extract the last digit of the
        # number and check if it is
        # equal to the given digit (d)
        remainder = temp % 10
        temp = temp // 10
        count += 1
 
        if (remainder == d):
            # If the digit which
            # need to be replaced
            # is equal to 0 then
            # increment the current
            # digit and make the all digits
            # to its right 1
            if (d == 0):
 
                num = '1'*count
                temp = temp * pow(10, count) + int(num)
 
            # Else, increment the current digit
            # by 1 and make the all digits
            # to its right 0
            else:
 
                temp = (temp * pow(10, count) + (remainder + 1)
                             * pow(10, count - 1))
 
            # After the removal of given
            # digit the number will be temp
            # So, the required smallest
            # number is (temp - n)
            ans = temp - n
 
            # Set count as 0
            count = 0
 
    # Return the result
    return ans
 
# Driver code
if __name__ == "__main__":
 
    N = 100
    D = 0
 
    # print the smallest number required
    # to be added to remove
    # the digit 'D' in number 'N'
    print(smallestNumber(N, D))
 
    # This code is contributed by ukasp.

C#




// C# implementation for the above approach
using System;
 
class GFG {
 
  // Function to calculate smallest positive
  // number to be added to remove
  // the given digit
  public static int smallestNumber(int n, int d)
  {
 
    // Copy the number to another variable
    int temp = n;
 
    int ans = 0, count = 0;
 
    // Iterate over digits of the given
    // number from right to left
    while (temp > 0)
    {
 
      // Extract the last digit of the
      // number and check if it is
      // equal to the given digit (d)
      int remainder = temp % 10;
      temp = temp / 10;
      count++;
 
      if (remainder == d)
      {
 
        // If the digit which need to be
        // replaced is equal to 0 then
        // increment the current digit
        // and make the all digits to
        // its right 1
        if (d == 0)
        {
          string num = "";
          for(int i = 0; i < count; i++)
          {
            num = num + "1";
          }
          temp = (int)(temp * Math.Pow(10, count) +
                       Int32.Parse(num));
        }
 
        // Else, increment the current digit
        // by 1 and make the all digits
        // to its right 0
        else
        {
          temp = (int) (temp * Math.Pow(10, count) +
                        (remainder + 1) * Math.Pow(10, count - 1));
        }
 
        // After the removal of given
        // digit the number will be temp
        // So, the required smallest
        // number is (temp - n)
        ans = temp - n;
 
        // Set count as 0
        count = 0;
      }
    }
 
    // Return the result
    return ans;
  }
 
  // Driver code
  public static void Main(String[] args)
  {
 
    int N = 100;
    int D = 0;
 
    // Print the smallest number required
    // to be added to remove the digit 'D'
    // in number 'N'
    Console.Write(smallestNumber(N, D));
  }
}
 
// This code is contributed by sanjoy_62.

Javascript




<script>
       // JavaScript Program to implement
       // the above approach
 
       // Function to calculate smallest positive
       // number to be added to remove
       // the given digit
       function smallestNumber(n, d)
       {
        
           // Copy the number to another variable
           let temp = n;
 
           let ans = 0, count = 0;
 
           // Iterate over digits of the given
           // number from right to left
           while (temp > 0) {
 
               // Extract the last digit of the
               // number and check if it is
               // equal to the given digit (d)
               let remainder = temp % 10;
               temp = Math.floor(temp / 10);
               count++;
 
               if (remainder == d)
               {
                
                   // If the digit which
                   // need to be replaced
                   // is equal to 0 then
                   // increment the current
                   // digit and make the all digits
                   // to its right 1
                   if (d == 0) {
 
                       let num = ''
                       for (let i = 0; i < count; i++) {
                           num = num + '1';
                       }
                       temp = temp * Math.pow(10, count) + parseInt(num);
                   }
 
                   // Else, increment the current digit
                   // by 1 and make the all digits
                   // to its right 0
                   else {
 
                       temp = temp * Math.pow(10, count)
                           + (remainder + 1)
                           * Math.pow(10, count - 1);
                   }
                    
                   // After the removal of given
                   // digit the number will be temp
                   // So, the required smallest
                   // number is (temp - n)
                   ans = temp - n;
 
                   // Set count as 0
                   count = 0;
               }
           }
 
           // Return the result
           return ans;
       }
 
       // Driver code
       let N = 100;
       let D = 0;
 
       // print the smallest number required
       // to be added to remove
       // the digit 'D' in number 'N'
       document.write(smallestNumber(N, D) + "<br>");
 
   // This code is contributed by Potta Lokesh
   </script>
Output
11

Time Complexity: O((log(N))^2)
Auxiliary Space: O(log(N))

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!