Game of Stones - Count the ways

183 Views Asked by At

We are given a number of piles of stones.
and we can remove two stones , where both stones come from different piles.
We do this until all the piles are finished or only one pile is left as we cannot do the move.
Now we have to tell the number of different configurations we can get at the end.
Eg :-
2 1 1
Different Configuration = 2 , Type 1 : 0 0 0 , Type 2 : 2 0 0

1 1 1
Different Configuration = 1 , Type 1 : 1 0 0 or 0 1 0 or 0 0 1

1

There are 1 best solutions below

0
On

As each move removes an even number (two) of stones, the ending position must have a single pile of the same parity as the total of all the starting piles. If one pile starts out with over half the stones, the minimum number of stones left is the difference between the largest pile and the sum of all the other piles. This is achieved by removing one stone from the large pile and one stone from some other pile. The maximum number of stones is a little more complicated. As long as the second largest pile can get down to $0$ or $1$ in the game ignoring the largest pile, the largest can lose $0$ or $1$ stones to keep the parity. If not, the largest pile must lose the minimum number of stones left in the second largest pile.