Closest
Consider two n-digit positive decimal integers A and B with no leading zeroes. We need to find the two closest to A n-digit numbers (the first one – greater or equal to A, the other – strictly less than A), with decimal writings containing all the digits of B in some order.
For example if A = 3022 and B = 1232, using B’s digits we can obtain the following 4-digit numbers: 1223, 1232, 1322, 2123, 2132, 2213, 2231, 2312, 2321, 3122, 3212 and 3221. The least number greater or equal to A obtained by B’s digits is 3122, and the biggest one, strictly less then A is 2321. If A = 1232 and B = 3022, the possible numbers are 2023, 2032, 2203, 2230, 2302, 2320, 3022, 3202 and 3220. The least number greater or equal to A obtained by B’s digits is 2023, and there is no number less than A. Write a program closest to find these “closest to A” numbers for given A and B, or to determine that one of them does not exist.
- Line 1: the least n-digit number with no leading zeros, not less than A, containing all the digits of B in some order. If such number does not exist, the output should be 0.
- Line 2: the biggest n-digit number with no leading zeros, less than A, containing all the digits of B in some order. If such number does not exist, the output should be 0.
3075
6604Output:
4066
03000203
4562454Output:
4244556
2655444Submit solution
Coming laterThe grading service will be connected in a later migration step. You can inspect the task and your previous results now.