Problem Statement
Suppose you have pebbles arranged into several piles. On each round you take one pebble from each pile and make a new pile out of the pebbles you took. As the number of ways to divide the pebbles into piles is finite, it is obvious that arrangements of piles will start to repeat themselves.
Your task is, given an arrangement of pebbles into piles, return the first round when the cycle starts. Consider the original arrangement as round zero.
Comment
This problem is based on the following math problem. You have an arrangement
with 45 pebbles, prove that after several rounds it stabilizes (i.e. the final cycle is actually an arrangement that repeats itself at each round). This math
problem sounds easy, but in fact it is not that trivial.
Definition
- Class:
- Pebbles
- Method:
- cycleStart
- Parameters:
- int[]
- Returns:
- int
- Method signature:
- int cycleStart(int[] initialPiles)
- (be sure your method is public)
Notes
- The arrangement doesn't depend on how you order the piles. Namely, the arrangement {3, 2, 1} is considered the same as {1, 2, 3}.
Constraints
- initialPiles has between 1 and 20 elements inclusive
- each element in initialPiles is between 1 and 100 inclusive
Examples
{1,2,3,4,5,6,7,8,9}
Returns: 0
If we take one pebble away from each of the piles to form a new pile, we will have a new pile with 9 pebbles. Each of the other piles has one less pebble than originally, in particular, the pile which had 1 pebble disappears. Thus, we end up with piles with the same numbers of pebbles that we started with - {1,2,3,4,5,6,7,8,9}.
{5}
Returns: 2
We take one pebble from the only pile and make a new pile with it. Thus, on the next round we have the arrangement {1, 4}. On the next round we have {2, 3}, the next round - {1, 2, 2}, the next round - {1, 1, 3}, and the next round - {2, 3}. The last arrangement of piles is the same as round two, return 2.
{100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100}
Returns: 2762
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
Returns: 15
{10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100}
Returns: 246
{100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,99}
Returns: 2698
{100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,99,99}
Returns: 2634
{100,100,100,100,100,100,100,100,100,100,100,100,100,100,99,99,99,99,99,99}
Returns: 2378
{99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99}
Returns: 1988
{1,1,1,1,1,1,1,1,1,1,100,100,100,100,100,100,100,100,100,100}
Returns: 910
{100,99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,82,81}
Returns: 2200
{100,99,98,97,96,95,94,93,92,91,100,99,98,97,96,95,94,93,92,91}
Returns: 2381
{6}
Returns: 3
{55}
Returns: 45
{1}
Returns: 0
{2}
Returns: 0
{1,1}
Returns: 0
{9,8,7,6,5,4,3,2,1}
Returns: 0
{99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99}
Returns: 1988
{100}
Returns: 86
{ 100, 99, 75 }
Returns: 358
{ 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 }
Returns: 2762
{ 5 }
Returns: 2
{ 45 }
Returns: 36
{ 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 1, 1, 1 }
Returns: 1180
{ 1 }
Returns: 0
{ 1, 4 }
Returns: 1