Problem Statement
Given are small positive integers D and S.
Return the smallest positive integer with the following properties:
- It has exactly D digits.
- The sum of its digits is exactly S.
- The product of its digits is zero.
If no such integer exists, return -1 instead.
Definition
- Class:
- GivenDigitSum
- Method:
- construct
- Parameters:
- int, int
- Returns:
- long
- Method signature:
- long construct(int D, int S)
- (be sure your method is public)
Notes
- Everything in this problem is in base 10.
- The leading digit of a positive integer must always be non-zero.
Constraints
- D will be between 1 and 18, inclusive.
- S will be between 1 and 200, inclusive.
Examples
1
7
Returns: -1
The only 1-digit number with sum of digits 7 is obviously the number 7. However, the product of its digits isn't zero (it's 7), so there is no integer with all three required properties.
3
11
Returns: 209
Another integer with all three desired properties is 470: it has 3 digits, its digit sum is 4+7+0 = 11, and its digit product is 4*7*0 = 0. Remember that if multiple integers with the desired properties exist, you must return the smallest one among them. Thus, here only the answer 209 is accepted.
18
1
Returns: 100000000000000000
Watch out for integer overflow. The answer sometimes won't fit into a 32-bit integer variable.
11
11
Returns: 10000000019
3
99
Returns: -1
There is no 3-digit integer with digit sum 99.
1
1
Returns: -1
1
9
Returns: -1
1
10
Returns: -1
2
1
Returns: 10
2
7
Returns: 70
2
9
Returns: 90
2
10
Returns: -1
3
1
Returns: 100
3
6
Returns: 105
3
10
Returns: 109
3
11
Returns: 209
3
15
Returns: 609
3
18
Returns: 909
3
19
Returns: -1
3
47
Returns: -1
4
2
Returns: 1001
4
25
Returns: 7099
4
27
Returns: 9099
4
36
Returns: -1
18
200
Returns: -1
13
100
Returns: 1099999999999
13
47
Returns: 1000000199999
18
153
Returns: 909999999999999999
18
154
Returns: -1
7
18
Returns: 1000089