Statistics

Problem Statement for "Reroll"

Problem Statement

This problem is about dice. All dice in this problem are the standard six-sided dice with numbers 1 through 6 on their faces.

Consider the following game that is played with n dice:

  1. Your opponent chooses a positive integer target (between n and 6n, inclusive).
  2. You roll all n dice.
  3. You select any subset of the dice you rolled and you reroll them.
  4. You add the numbers that are now shown on the n dice. If the sum is exactly equal to target, you win, otherwise you lose.

This game is often played as a drinking game with one extra rule: for each die you rerolled in step 3 you have to drink a shot.

Monika is playing this game. Her priorities are as follows:

  1. She wants to have a chance to win the game.
  2. She wants to drink as few shots as possible.

You are given the int target. You are also given the int[] dice. The elements of dice are the values rolled by Monika in step 2 of the game. Monika is now in step 3. Compute and return the smallest number of shots she will have to drink in order to have a positive probability to win the game.

Definition

Class:
Reroll
Method:
minimumDice
Parameters:
int, int[]
Returns:
int
Method signature:
int minimumDice(int target, int[] dice)
(be sure your method is public)

Constraints

  • target will be between n and 6n, inclusive, where n is the number of elements in dice.
  • dice will contain between 1 and 20 elements, inclusive.
  • Each element of dice will be between 1 and 6, inclusive.

Examples

  1. 7

    {2,6,4}

    Returns: 1

    The current sum is 12. In order to have a shot at having sum of 7, Monika has to reroll the 6 and hope for a 1.

  2. 10

    {4,2,4,5}

    Returns: 2

    The current sum is 15. Monika can choose, for instance, to reroll the two fours. She will win if she gets a 1 and a 2.

  3. 42

    {6,6,6,6,6,6,6}

    Returns: 0

    Monika has already won - no need to reroll anything.

  4. 42

    {1,3,5,3,6,4,2}

    Returns: 6

    Out of all the dice, just the single 6 is useful. Monika has to reroll all other dice and hope they all land a six.

  5. 120

    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}

    Returns: 20

  6. 20

    {6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6}

    Returns: 20

  7. 3

    {1}

    Returns: 1

  8. 2

    {3}

    Returns: 1

  9. 6

    {5}

    Returns: 1

  10. 2

    {2}

    Returns: 0

  11. 13

    {6,5,4,5,3,3,1,6,5,4}

    Returns: 8

  12. 21

    {5,4,1,3,3,2,6,1,3,1,2,2,6,6,4,1,2,6,3,6}

    Returns: 15

  13. 95

    {5,3,2,3,4,1,4,6,6,5,5,4,4,6,5,4,5,1,1,4}

    Returns: 4

  14. 49

    {6,2,6,2,1,1,5,2,4,5,1,2,2,4,3,4,1,1,4,4}

    Returns: 3

  15. 24

    {1,3,2,4,6,3,6,3,1,3,6,4,3,2,4,4,2,6,2,3}

    Returns: 14

  16. 34

    {3,6,4,5,6,3,3,4,2,4,3,2,5,2,1,4,4,1,2,1}

    Returns: 9

  17. 83

    {6,1,3,4,2,1,4,3,5,2,1,4,2,3,3,2,4,5,1,3}

    Returns: 5

  18. 51

    {4,6,4,6,6,4,5,3,4,3,2,5,2,3,6,3,6,3,6,3}

    Returns: 7

  19. 47

    {1,4,3,5,2,3,1,2,5,3,6,3,2,5,5,3,3,6,2,6}

    Returns: 5

  20. 58

    {3,1,3,2,4,3,1,5,2,3,6,5,2,3,4,2,3,5,3,4}

    Returns: 2

  21. 106

    {3,4,2,4,3,2,3,5,5,3,4,1,3,6,2,2,6,4,3,1}

    Returns: 11

  22. 34

    {2,3,2,6,6,2,4,2,2,2,5,2,1,2,5,3,6,5,6,2}

    Returns: 8

  23. 24

    {3,2,6,6,5,2,2,1,4,4}

    Returns: 3

  24. 44

    {4,6,6,4,5,5,6,2,2,5}

    Returns: 1

  25. 42

    {4,3,4,3,6,6,2,4,4,1}

    Returns: 1

  26. 28

    {4,1,5,6,3,5,3,2,2,5}

    Returns: 2

  27. 25

    {6,3,6,5,4,5,5,3,5,3}

    Returns: 5

  28. 43

    {3,3,3,6,4,6,5,5,6,5}

    Returns: 1

  29. 60

    {2,1,2,6,5,5,2,6,4,3}

    Returns: 8

  30. 43

    {1,2,6,6,2,4,2,6,2,3}

    Returns: 2

  31. 57

    {5,2,1,5,4,1,3,1,2,1}

    Returns: 8

  32. 25

    {1,1,4,5,4}

    Returns: 2

  33. 8

    {3,5,6,2,3}

    Returns: 3

  34. 14

    {6,4,2,5,4}

    Returns: 2

  35. 12

    {6,3,3,4,2}

    Returns: 2

  36. 30

    {4,2,6,4,2}

    Returns: 4

  37. 3

    {3,2}

    Returns: 1

  38. 5

    {2,5}

    Returns: 1

  39. 10

    {4,3}

    Returns: 1

  40. 12

    {2,4}

    Returns: 2

  41. 2

    {1,2}

    Returns: 1

  42. 2

    {2,4}

    Returns: 2

  43. 2

    {5,3}

    Returns: 2

  44. 9

    {2,3}

    Returns: 1

  45. 7

    {1,4}

    Returns: 1

  46. 2

    {2,6}

    Returns: 2

  47. 2

    {1,5}

    Returns: 1

  48. 12

    {1,1}

    Returns: 2

  49. 3

    {4,5}

    Returns: 2

  50. 78

    {5,4,5,5,6,2,6,5,5,4,2,5,2,4,3,5,6,2,1}

    Returns: 1

  51. 95

    {4,6,2,2,6,4,3,6,5,2,6,1,1,3,5,5,5,5,4}

    Returns: 5

  52. 67

    {1,6,1,3,6,3,4,6,5,3,4,4,6,3,6,3,1,2}

    Returns: 0

  53. 79

    {2,3,1,3,6,5,4,5,1,2,6,5,5,5,1,5,3,6}

    Returns: 3

  54. 68

    {6,3,1,3,4,1,5,1,2,4,2,1,5,5,5,3,4}

    Returns: 3

  55. 63

    {4,2,3,1,4,5,2,4,5,6,4,4,5,2,2,3,5}

    Returns: 1

  56. 82

    {1,6,2,4,5,2,1,5,3,4,2,1,3,2,4,6}

    Returns: 7

  57. 40

    {2,1,6,3,4,5,5,4,4,4,5,3,6,3,4,5}

    Returns: 6

  58. 24

    {4,5,3,4,3,4,2,5,6,3,2,6,6,5,2}

    Returns: 9

  59. 17

    {1,6,1,4,4,1,4,2,3,3,3,2,3,3,2}

    Returns: 10

  60. 42

    {1, 3, 5, 3, 6, 4, 2 }

    Returns: 6

  61. 10

    {4, 2, 4, 5 }

    Returns: 2

  62. 12

    {1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 }

    Returns: 11

  63. 1

    {6 }

    Returns: 1

  64. 12

    {1, 2, 3, 4 }

    Returns: 1

  65. 7

    {2, 6, 4 }

    Returns: 1

  66. 10

    {6, 6, 6, 6, 6, 6, 6 }

    Returns: 7

  67. 32

    {6, 6, 6, 6, 6, 6, 6, 1 }

    Returns: 3

  68. 8

    {2, 1, 6 }

    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: