SmallestOppositeNumber
SRM 831 · 2022-06-06 · by misof
Problem Statement
Given is a non-negative integer X.
Construct and return the smallest non-negative integer Y such that (in base 10) each digit that does not appear in X does appear in Y.
Note that Y is not allowed to start with unnecessary leading zeros. (See Examples 2 and 3.)
Notes
- The constraints guarantee that X has at most 9 digits. Thus, at least one of the digits 0-9 will be missing from X.
Constraints
- X will be between 0 and 999,999,999, inclusive.
2024868 Returns: 13579
The digits 0, 2, 4, 6 and 8 appear in the given number X. (Some of them appear more than once, but that does not matter.) The digits that do not appear in the given number X are the digits 1, 3, 5, 7 and 9. All these digits must therefore appear in Y. The smallest non-negative integer that contains these five digits is clearly Y = 13,579.
0 Returns: 123456789
The number X = 0 contains the digit 0. The number Y must contain the digits 1-9. The smallest such Y is obviously 123,456,789.
123456798 Returns: 0
This is almost an inverse of the previous example: X contains all digits other than 0, so Y should contain the digit 0. The smallest non-negative integer that contains the digit 0 is the number 0.
45678 Returns: 10239
Remember that the number Y cannot start with an unnecessary leading zero. Thus, "01239" is not a valid answer. The smallest number that really contains the digits 0, 1, 2, 3, 9 is 10,239.
1 Returns: 203456789
Submissions are judged against all 36 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class SmallestOppositeNumber with a public method int construct(int X) · 36 test cases · 2 s / 256 MB per case