Statistics

Problem Statement for "Straights"

Problem Statement

You are playing a game of cards in which the number of straights, i.e., sets of consecutive-valued cards, determines the strength of your hand. You will be given a int[] hand, where the i-th element of hand is the number of cards of value i in your hand. You should return the number of straights of length k. For example, suppose you have the hand:
  • 2 of spades
  • 2 of diamonds
  • 2 of clubs
  • 3 of clubs
  • 4 of hearts
  • 4 of clubs
You would be given hand = { 0, 3, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 }. The number of two-card straights is 5:
  • 2 of spades, 3 of clubs
  • 2 of diamonds, 3 of clubs
  • 2 of clubs, 3 of clubs
  • 3 of clubs, 4 of hearts
  • 3 of clubs, 4 of clubs

Definition

Class:
Straights
Method:
howMany
Parameters:
int[], int
Returns:
int
Method signature:
int howMany(int[] hand, int k)
(be sure your method is public)

Notes

  • Straights do not wrap around: if hand is {1,0,0,0,0,0,0,0,0,0,0,0,1}, you have no straights of length 2.

Constraints

  • hand will contain exactly 13 elements.
  • Each element of hand will be between 0 and 4 inclusive.
  • k will be between 1 and 13 inclusive.

Examples

  1. {0,3,1,2,0,0,0,0,0,0,0,0,0}

    2

    Returns: 5

    The example given.

  2. {1,1,1,1,1,1,1,1,1,1,1,1,1}

    5

    Returns: 9

    Say hand[0] references Aces. There are 9 ways to make a straight of length 5: Ace-Five up to Nine-King.

  3. {4,4,4,4,4,4,4,4,4,4,4,4,4}

    13

    Returns: 67108864

  4. {4,0,4,0,4,0,4,0,4,0,4,0,4}

    2

    Returns: 0

    Straights do not wrap around; we have no straights here.

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

    1

    Returns: 31

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

    3

    Returns: 79

  7. {2,2,3,0,1,1,4,2,2,3,2,1,3}

    10

    Returns: 0

  8. {4,2,3,4,4,3,2,4,3,4,2,3,1}

    8

    Returns: 47808

  9. {0,2,3,4,4,2,0,0,1,0,3,2,4}

    13

    Returns: 0

  10. {2,4,2,0,3,4,4,1,1,1,1,0,1}

    5

    Returns: 68

  11. {1,4,1,4,1,3,3,1,4,3,4,0,2}

    6

    Returns: 912

  12. {4,0,2,3,0,1,1,1,0,0,4,2,3}

    12

    Returns: 0

  13. {0,0,3,2,0,4,4,3,2,0,3,2,2}

    11

    Returns: 0

  14. {4,1,1,4,4,3,4,3,2,1,2,3,0}

    4

    Returns: 536

  15. {0,3,4,2,1,2,2,4,0,4,2,2,3}

    10

    Returns: 0

  16. {1,1,0,0,0,4,1,4,2,2,1,4,3}

    6

    Returns: 320

  17. {1,1,2,2,0,2,0,0,3,3,0,2,1}

    8

    Returns: 0

  18. {1,1,2,3,1,1,4,3,3,2,3,3,4}

    2

    Returns: 71

  19. {2,0,0,3,4,1,1,0,3,4,4,4,1}

    6

    Returns: 0

  20. {1,4,0,3,4,1,2,1,4,4,1,4,3}

    12

    Returns: 0

  21. {0,1,1,1,0,3,1,1,0,3,2,4,3}

    11

    Returns: 0

  22. {4,4,4,3,4,0,0,3,1,2,4,4,4}

    11

    Returns: 0

  23. {3,3,1,3,0,3,2,3,1,1,2,0,2}

    4

    Returns: 57

  24. {1,3,3,3,3,3,0,4,3,1,3,3,2}

    10

    Returns: 0

  25. {1,2,4,0,1,1,2,3,1,2,3,0,0}

    7

    Returns: 36

  26. {0,4,3,3,2,2,2,2,1,1,1,4,0}

    13

    Returns: 0

  27. {3,0,1,3,4,1,4,0,2,0,4,3,4}

    5

    Returns: 48

  28. {3,3,0,0,2,1,0,3,0,3,3,2,2}

    13

    Returns: 0

  29. {0,4,1,2,4,2,3,3,1,4,3,2,0}

    7

    Returns: 2592

  30. {3,0,4,0,4,2,3,0,2,1,4,0,1}

    4

    Returns: 0

  31. {0, 3, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 }

    2

    Returns: 5

  32. {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1 }

    1

    Returns: 31

  33. {4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 }

    13

    Returns: 67108864

  34. {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }

    5

    Returns: 288

  35. {0, 3, 1, 2, 3, 2, 2, 0, 0, 0, 0, 0, 0 }

    4

    Returns: 54

  36. {1, 2, 3, 4, 4, 3, 2, 1, 4, 2, 4, 2, 3 }

    5

    Returns: 1232


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: