Practice on Toph

Participate in exhilarating programming contests, solve unique algorithm and data structure challenges and be a part of an awesome community.

My Small Number

By IamHot · Limits 1s, 512 MB

You are given two integers N and M. Find another integer X divisible by M that has the same number of digits as N. If there are multiple such X, then print the one with the least number of digits different from N. If there are still multiple such X, then print the smallest one among them.

Here, the same number of digits mean the number of positions where there the two numbers have same digits. For example, the numbers "123" and "321" have 1 same digit in the second position.

The length of a number will be considered without leading zeroes.


Input contains two space-separate integers N and M without leading zeroes.

  • 10 ≤ N ≤ 10100
  • 1 ≤ M ≤ 105 , M ≤ N


Output one line containing integer X as described above, without leading zeroes.


10 5
10 7



44% Solution Ratio

Optimised_TLEEarliest, Nov '19

SaikatSFastest, 0.4s

NAbdullaLightest, 39 MB

zarif2Shortest, 956B


Login to submit

Toph uses cookies. By continuing you agree to our Cookie Policy.