Statistics

Problem Statement for "GivenDigitSum"

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. 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.

  2. 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.

  3. 18

    1

    Returns: 100000000000000000

    Watch out for integer overflow. The answer sometimes won't fit into a 32-bit integer variable.

  4. 11

    11

    Returns: 10000000019

  5. 3

    99

    Returns: -1

    There is no 3-digit integer with digit sum 99.

  6. 1

    1

    Returns: -1

  7. 1

    9

    Returns: -1

  8. 1

    10

    Returns: -1

  9. 2

    1

    Returns: 10

  10. 2

    7

    Returns: 70

  11. 2

    9

    Returns: 90

  12. 2

    10

    Returns: -1

  13. 3

    1

    Returns: 100

  14. 3

    6

    Returns: 105

  15. 3

    10

    Returns: 109

  16. 3

    11

    Returns: 209

  17. 3

    15

    Returns: 609

  18. 3

    18

    Returns: 909

  19. 3

    19

    Returns: -1

  20. 3

    47

    Returns: -1

  21. 4

    2

    Returns: 1001

  22. 4

    25

    Returns: 7099

  23. 4

    27

    Returns: 9099

  24. 4

    36

    Returns: -1

  25. 18

    200

    Returns: -1

  26. 13

    100

    Returns: 1099999999999

  27. 13

    47

    Returns: 1000000199999

  28. 18

    153

    Returns: 909999999999999999

  29. 18

    154

    Returns: -1

  30. 7

    18

    Returns: 1000089


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: