TopcoderArchive
Archive
Algorithm problem

DeckShuffler

Problem statement, definition, constraints, and public examples.

Problem Statement

You have a card shuffling machine that takes a stack of m cards and reorders them according to the sequence given in shuffled. For example, if the third element of shuffled is 7, then the 7th card of the input will be the third card of the output. You will take the sequence of cards 1, 2, 3, ..., m, and feed it into the shuffling machine n times. Each time (except for the first time), you will feed the resulting sequence from the previous time back into the machine. Return the resulting sequence after the last run of the machine.

Definition

Class:
DeckShuffler
Method:
getDeck
Parameters:
int, int[]
Returns:
int[]
Method signature:
int[] getDeck(int n, int[] shuffled)
(be sure your method is public)

Constraints

  • n will be between 1 and 100, inclusive.
  • Each element of shuffled will be between 1 and m, inclusive, where m is the number of elements in shuffled.
  • shuffled will contain between 1 and 50 elements, inclusive.
  • Each element of shuffled will be distinct.

Examples

  1. 10
    {1,2,3,4}
    Returns: {1, 2, 3, 4 }
    The machine doesn't actually change any positions so we return {1,2,3,4}.
  2. 3
    {2,1,3,4,5}
    Returns: {2, 1, 3, 4, 5 }
    The machine swaps the first two cards. Performing this procedure 3 times gives the resulting sequence {2,1,3,4,5}.
  3. 3
    {3,1,2}
    Returns: {1, 2, 3 }
    The machine cyclically permutes the cards, so shuffling 3 times is like not shuffling at all. We return {1,2,3}.
  4. 100
    {2,1,4,5,3,7,8,9,6,11,12,13,14,10}
    Returns: {1, 2, 4, 5, 3, 6, 7, 8, 9, 10, 11, 12, 13, 14 }
  5. 13
    {2,1,4,5,3,7,8,9,6,11,12,13,14,10}
    Returns: {2, 1, 4, 5, 3, 7, 8, 9, 6, 13, 14, 10, 11, 12 }
  6. 1
    {3,1,5,2,4}
    Returns: {3, 1, 5, 2, 4 }
Back to all problems