Digit Removal CodeChef Solution

Hello Programmers In this post, you will know how to solve the Digit Removal CodeChef Solution which is a part of CodeChef Solutions.

Digit Removal CodeChef Solution
Digit Removal CodeChef Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Problem

You are given an integer N and a digit D. Find the minimum integer you should add to N such that the final value of N does not contain the digit D.

Input

  • The first line contains T denoting the number of test cases. Then the test cases follow.
  • Each test case contains two integers N and D on a single line denoting the original number and the digit you need to avoid.

Output

For each test case, output on a single line the minimum integer you should add to N.

Constraints

  • 1 ≤ T ≤ 105
  • 1 ≤ N ≤ 109
  • 0 ≤ D ≤ 9

Subtasks

Subtask 1 (100 points): Original constraints

Example

Input:
5
21 5
8 8
100 0
5925 9
434356 3


Output:
1
11
75
5644

Explanation

Test case 1: N = 21 does not contain the digit D = 5. Hence there is no need to add any integers to N.

Test case 2: If 1 is added to N = 8, it becomes equal to 9, which does not contain the digit D = 8.

Test case 3: The minimum integer you should add to N = 100 such that the final value of N does not contain the digit D = 0 is 11.

Test case 5: The minimum integer which is greater than 434356 and does not contain the digit D = 3 is 440000. So we should add 440000 − 434356 = 5644……..

Digit Removal CodeChef Solution in JAVA

/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		while(t-->0)
		{
		    int n=sc.nextInt();
		    int d=sc.nextInt();
		    int newn = n;
    int f_digit,ans=0,p=0;
    while( newn >0 ){
        f_digit = newn % 10;
        newn /=  10;
        p++;
        if(f_digit==d){
            newn = newn * (int)Math.pow(10, p);
            newn += (f_digit + 1) * (int)Math.pow(10, p-1);
            ans = newn - n;
            p =0;
        }
    }
    System.out.println(ans);
		}
	}
}

Ezoicreport this adDigit Removal CodeChef Solution in CPP

#include<bits/stdc++.h>
using namespace std;
int isMin(int n, int d){
    int newN = n, rem , count =0, c = 0;
    while(newN>0){
        rem = newN % 10;
        newN = newN /10;
        c++;
        if(rem == d){
            newN = newN*pow(10,c)+(rem+1)*pow(10,c-1);
            count = newN -n;
            c = 0;
        }
    }
    return count;
}
int main()
{
  int t;
  cin>>t;
  while(t--){
      int n, d;
      cin>>n>>d;
      cout<<isMin(n, d) <<endl;
  }
  return 0;
}

Digit Removal CodeChef Solutions in Python

# cook your dish here
def counting(idx, k, num, s, count):
    for j in range(idx + 1, k):
        count = count * 10 + (num - int(s[j]))
    return count
def nine(a):
    i=a.find('9')
    if i==0:
        return str(pow(10,len(a)))
    else:
        return a[:i-1]+str(int(a[i-1])+1)+('0'*len(a[i:len(a)]))
for _ in range(int(input())):
    n, d = map(int, input().split())
    s = str(n)
    count = 0
    if (n == d):
        print("1")
    else:
        idx = s.find(str(d))
        if (idx == -1):
            print("0")
        else:
            k = len(s)
            if (d == 0):
                count = counting(idx, k, 10, s, 0)
                print(count + 1)
            else:
                if (d!=9):
                    count = counting(idx, k, 9, s, 0)
                    print(count + 1)
                else:
                    t=False
                    if '9' in s:
                        t=True
                    dup=s[:]
                    while(t==True):
                        dup=nine(dup)
                        if '9' not in dup:
                            t=False
                    print(int(dup)-n)
# cook your dish here
Ezoicreport this ad

Disclaimer: The above Problem (Digit Removal CodeChef) is generated by CodeChef but the solution is provided by BrokenProgrammers. This tutorial is only for Educational and Learning purpose.

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Next: Which Fuel is Cheaper CodeChef Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad