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++
#include <bits/stdc++.h>
using namespace std;
int smallestNumber( int n, int d)
{
int temp = n;
int ans = 0, count = 0;
while (temp > 0) {
int remainder = temp % 10;
temp = temp / 10;
count++;
if (remainder == d) {
if (d == 0) {
string num = string(count, '1' );
temp = temp * pow (10, count) + stoi(num);
}
else {
temp = temp * pow (10, count)
+ (remainder + 1)
* pow (10, count - 1);
}
ans = temp - n;
count = 0;
}
}
return ans;
}
int main()
{
int N = 100;
int D = 0;
cout << smallestNumber(N, D) << "\n" ;
return 0;
}
|
Java
class GFG{
public static int smallestNumber( int n, int d)
{
int temp = n;
int ans = 0 , count = 0 ;
while (temp > 0 )
{
int remainder = temp % 10 ;
temp = temp / 10 ;
count++;
if (remainder == d)
{
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
{
temp = ( int ) (temp * Math.pow( 10 , count) +
(remainder + 1 ) * Math.pow( 10 , count - 1 ));
}
ans = temp - n;
count = 0 ;
}
}
return ans;
}
public static void main(String args[])
{
int N = 100 ;
int D = 0 ;
System.out.println(smallestNumber(N, D));
}
}
|
Python3
def smallestNumber(n, d):
temp = n
ans = 0
count = 0
while (temp > 0 ):
remainder = temp % 10
temp = temp / / 10
count + = 1
if (remainder = = d):
if (d = = 0 ):
num = '1' * count
temp = temp * pow ( 10 , count) + int (num)
else :
temp = (temp * pow ( 10 , count) + (remainder + 1 )
* pow ( 10 , count - 1 ))
ans = temp - n
count = 0
return ans
if __name__ = = "__main__" :
N = 100
D = 0
print (smallestNumber(N, D))
|
C#
using System;
class GFG {
public static int smallestNumber( int n, int d)
{
int temp = n;
int ans = 0, count = 0;
while (temp > 0)
{
int remainder = temp % 10;
temp = temp / 10;
count++;
if (remainder == d)
{
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
{
temp = ( int ) (temp * Math.Pow(10, count) +
(remainder + 1) * Math.Pow(10, count - 1));
}
ans = temp - n;
count = 0;
}
}
return ans;
}
public static void Main(String[] args)
{
int N = 100;
int D = 0;
Console.Write(smallestNumber(N, D));
}
}
|
Javascript
<script>
function smallestNumber(n, d)
{
let temp = n;
let ans = 0, count = 0;
while (temp > 0) {
let remainder = temp % 10;
temp = Math.floor(temp / 10);
count++;
if (remainder == d)
{
if (d == 0) {
let num = ''
for (let i = 0; i < count; i++) {
num = num + '1' ;
}
temp = temp * Math.pow(10, count) + parseInt(num);
}
else {
temp = temp * Math.pow(10, count)
+ (remainder + 1)
* Math.pow(10, count - 1);
}
ans = temp - n;
count = 0;
}
}
return ans;
}
let N = 100;
let D = 0;
document.write(smallestNumber(N, D) + "<br>" );
</script>
|
Time Complexity: O((log(N))^2)
Auxiliary Space: O(log(N))