TopCoder problem "Straights" used in SRM 245 (Division II Level One)



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

0)
    
{0,3,1,2,0,0,0,0,0,0,0,0,0}
2
Returns: 5
The example given.
1)
    
{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.
2)
    
{4,4,4,4,4,4,4,4,4,4,4,4,4}
13
Returns: 67108864
3)
    
{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.
4)
    
{1,2,3,4,1,2,3,4,1,2,3,4,1}
1
Returns: 31

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=4561

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=7220&pm=4561

Writer:

Enogipe

Testers:

PabloGilberto , lbackstrom , brett1479 , vorthys

Problem categories:

Simple Math