A group of children are standing in a circle. They all start with a multiple of 3 candies each (not necessarily equal), and perform the following algorithm:
Step 1. Each child divides their candies equally into 3 piles. Proceed to step 2.
Step 2. Each child gives 1 pile to the person on their left and 1 pile to the person on their right. Keep the last pile. Proceed to step 3.
Step 3. Each child now counts the number of candies they have.
- If it's not a multiple of 3, grab candies from the candy bag one by one until it becomes a multiple of 3 and proceed to step 4.
- If it's already a multiple of 3, do nothing and proceed to step 4.
Step 4. At this point, if everyone has the same number of candies, stop, or else, repeat from step 1.
Assuming they have an infinite supply of candies from the candy bag, will this algorithm ever end?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Suppose in one of the iterations, among all children, those who have the most candies have 3 M candies, and those who have the least candies have 3 L candies.
Here are 2 facts to fill our tummies!
This can be shown as follows: Take any 3 adjacent people, assume they have 3 ( M − m 1 ) , 3 ( M − m 2 ) , 3 ( M − m 3 ) candies, here, 0 ⩽ m 1 , m 2 , m 3 ⩽ M , after step 2, the one in the middle will have 3 M − ( m 1 + m 2 + m 3 ) ⩽ 3 M candies. They may take extra candies, but it will not exceed 3 M .
From this, we can conclude that the total number of candies everyone has will not exceed 3 M × n = 3 M n .
To show this, assume the algorithm hasn't end yet, find 3 adjacent people where the one in the middle has 3 L candies and at least one of the other two has more than 3 L candies (we can always find these 3 people since if we can't, then everyone must have the same number of candies, which is a contradiction), suppose they have 3 l 1 , 3 L , 3 l 2 candies, here, at least one of l 1 and l 2 is strictly greater than L . After step 2, the one in the middle will have L + l 1 + l 2 ⩾ 3 L + 1 candies. They may grab extra candies, but it will not be less than 3 L + 3 . This shows that after an iteration, the total number of candies everyone has must at least increase by 3.
Now, if the sequence never ends, then we have a strictly increasing total number of candies that does not go beyond 3 M n . This is a contradiction. Hence, the algorithm must end after a certain number of iterations.