TopcoderArchive
SRM21 Dec 2006
SRM 331

SantaGifts

Problem statement, definition, constraints, and public examples.

Problem Statement

Santa Claus has come with a bag full of presents this year. A group of N kids stand in line, anxious to get their gifts. Santa Claus takes a present from his bag and gives it to the kid who is currently first in line. That kid then goes to the end of the line, unless he already has 4 gifts, in which case, he goes home. This process continues as long as there are presents in the bag and kids in the line.

You are given a String[] gifts containing the presents in Santa's bag, in the order they are given away. Return a String[] containing exactly N elements, where the k-th element is a single space separated list of presents received by the k-th kid, in the order he received them. Kid 0 is the first kid in the initial lineup, kid 1 is the second kid, etc.

Definition

Class:
SantaGifts
Method:
distribute
Parameters:
String[], int
Returns:
String[]
Method signature:
String[] distribute(String[] gifts, int N)
(be sure your method is public)

Constraints

  • gifts will contain between 1 and 50 elements, inclusive.
  • Each element of gifts will contain between 1 and 10 lowercase letters ('a'-'z'), inclusive.
  • N will be between 1 and 50, inclusive.

Examples

  1. {"ball","plane","robot","puzzle"}
    3
    Returns: {"ball puzzle", "plane", "robot" }
    The first kid gets a ball, the second one a plane, and the third one a robot. Then the first kid reaches the front of the line again and gets the last gift.
  2. {"ball","plane","robot","puzzle","bike"}
    1
    Returns: {"ball plane robot puzzle" }
    There is only one kid, so he will take the first four presents.
  3. {"ball","ball","plane","plane"}
    2
    Returns: {"ball plane", "ball plane" }
    In this case Santa Claus is fair and gives a ball and a plane to both kids.
  4. {"ball","plane","robot"}
    5
    Returns: {"ball", "plane", "robot", "", "" }
    Unfortunately, not all the kids will get gifts.
Back to all problems