Statistics

Problem Statement for "TakeSubstringGame"

Problem Statement

A positive integer n is written on a board, and two players take turns making moves.

In each move, the player selects a positive integer m that is a proper substring of the number currently written on the board. The number on the board is then decreased by m. A proper substring is a substring that doesn't equal the whole string.

For example, if a board contains the number 2309, a player can select m = 2, 3, 9, 23, 30, 230 or 309. Thus the possible moves from 2309 are 2000, 2079, 2279, 2286, 2300, 2306 and 2307.

A player who can't make a valid move loses the game.

Return the number m that the first player should select in his first move in order to win the game. If there are several such numbers, return the smallest among them. If the first player cannot win the game, return -1.

Definition

Class:
TakeSubstringGame
Method:
winningMove
Parameters:
int
Returns:
int
Method signature:
int winningMove(int n)
(be sure your method is public)

Constraints

  • n will be between 1 and 1000000, inclusive.

Examples

  1. 5

    Returns: -1

    A single-digit number doesn't have a proper substring, so there is no valid move for the first player. Thus, the second player wins.

  2. 10

    Returns: 1

    Subtracting 0 is not a valid move (m must be positive). So the only valid move is to subtract 1. Now the second player receives number 9 and can't make a move, providing victory for the first player.

  3. 17

    Returns: -1

    Subtracting 7 will leave the second player with number 10, which is favorable for him, as shown in example above. Therefore the first player should subtract 1 and leave 16. Similarly, the second player will move on to 15. Three more such pairs of moves and the first player will be facing number 11. He will then subtract 1 and lose as shown above. Thus the second player wins.

  4. 239

    Returns: 9

    The optimal first move is to subtract either 9 or 23, leaving 230 or 216. In case of a tie, you should return the smallest subtracted number which is 9.

  5. 566

    Returns: 66

    Subtract 66 and leave your opponent with 500, which is apparently a losing position.

  6. 23900

    Returns: -1

    No first move is advantageous for the first player.

  7. 1

    Returns: -1

  8. 2

    Returns: -1

  9. 6

    Returns: -1

  10. 8

    Returns: -1

  11. 9

    Returns: -1

  12. 11

    Returns: -1

  13. 12

    Returns: 1

  14. 18

    Returns: 1

  15. 19

    Returns: -1

  16. 20

    Returns: -1

  17. 21

    Returns: 1

  18. 50

    Returns: -1

  19. 64

    Returns: 4

  20. 99

    Returns: 9

  21. 100

    Returns: 10

  22. 101

    Returns: -1

  23. 110

    Returns: 1

  24. 131

    Returns: -1

  25. 133

    Returns: -1

  26. 176

    Returns: -1

  27. 178

    Returns: -1

  28. 200

    Returns: 2

  29. 203

    Returns: 20

  30. 239

    Returns: 9

  31. 241

    Returns: -1

  32. 243

    Returns: 2

  33. 362

    Returns: -1

  34. 365

    Returns: 3

  35. 367

    Returns: -1

  36. 369

    Returns: -1

  37. 400

    Returns: -1

  38. 555

    Returns: 55

  39. 600

    Returns: -1

  40. 987

    Returns: 87

  41. 1000

    Returns: 100

  42. 1098

    Returns: -1

  43. 1100

    Returns: -1

  44. 2007

    Returns: 2

  45. 3316

    Returns: -1

  46. 3323

    Returns: 33

  47. 3324

    Returns: -1

  48. 5660

    Returns: 660

  49. 9999

    Returns: 999

  50. 10000

    Returns: 1000

  51. 10532

    Returns: -1

  52. 26830

    Returns: 68

  53. 26832

    Returns: 26

  54. 26834

    Returns: -1

  55. 26836

    Returns: 2

  56. 26838

    Returns: -1

  57. 60708

    Returns: 708

  58. 100096

    Returns: 9

  59. 100098

    Returns: -1

  60. 100102

    Returns: 2

  61. 111046

    Returns: -1

  62. 111048

    Returns: -1

  63. 142962

    Returns: -1

  64. 142968

    Returns: 6

  65. 146238

    Returns: 238

  66. 147238

    Returns: -1

  67. 149876

    Returns: 6

  68. 173737

    Returns: -1

  69. 183222

    Returns: -1

  70. 239519

    Returns: 519

  71. 308443

    Returns: -1

  72. 341095

    Returns: -1

  73. 375375

    Returns: 375

  74. 398765

    Returns: 8765

  75. 400000

    Returns: -1

  76. 566239

    Returns: 66239

  77. 700050

    Returns: 50

  78. 702000

    Returns: 2000

  79. 777777

    Returns: 77777

  80. 800000

    Returns: -1

  81. 898989

    Returns: 98989

  82. 900000

    Returns: -1

  83. 901010

    Returns: 1010

  84. 987654

    Returns: 87654

  85. 997755

    Returns: 97755

  86. 999971

    Returns: 99971

  87. 999996

    Returns: 99996

  88. 999999

    Returns: 99999

  89. 1000000

    Returns: 100000

  90. 54215

    Returns: 4215

  91. 979539

    Returns: 79539

  92. 939959

    Returns: 39959

  93. 101037

    Returns: -1

  94. 555555

    Returns: 55555

  95. 999990

    Returns: 99990

  96. 100001

    Returns: -1


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: