n people are seated in a circle. A total of nk coins are distributed amongst the people, but not necessarily equally. A move is the transfer of a single coin between two adjacent people. Find an algorithm for making the minimum number of moves which result in everyone ending up with the same number of coins?
Solution
Label the people from 1 to n, with person i next to person i+1, and person n next to person 1. Let person i initially hold ci coins. Let di = ci - k.
It is not obvious how many moves are needed. Clearly at least 1/2 ∑ |di| are needed. But one may need more. For example, suppose the starting values of di are 0, 1, 0, -1, 0. Then one needs at least 2 moves, not 1.
Obviously ∑ di = 0, so not all di can be negative. Relabel if necessary so that d1 ≥= 0. Now consider X = |d1| + |d1 + d2| + |d1 + d2 + d3| + ... + |d1 + d2 + ... + dn-1|. Note first that X is zero iff all di are zero. Any move between i and i+1, except one between n and 1, changes X by 1, because only the term |d1 + d2 + ... + di| is affected. Thus if we do not make any moves between n and 1, then we need at least X moves to reach the desired final position (with all di zero).
Assume X > 1. We show how to find a move which reduces X by 1. This requires a little care to avoid specifying a move which might require a person with no coins to transfer one. We are assuming that d1 ≥ 0. Take the first i for which di+1 < 0. There must be such an i, otherwise all di would be non-negative, but they sum to 0, so they would all have to be zero, contradicting X > 0. If d1 + ... + di > 0, then we take the move to be a transfer from i to i+1. This will reduce |d1 + ... + di| by 1 and leave the other terms in X unchanged, so it will reduce X by 1. If d1 + ... + di is not strictly positive, then by the minimality of i we must have d1 = d2 = ... = di = 0. We know that di+1 < 0. Now find the first j > i+1 such that dj ≥ 0. There must be such a j, otherwise we would have ∑ dm < 0. We have d1 + ... + dj-1 < 0, so a transfer from j to j-1 will reduce |d1 + ... + dj-1| and hence reduce X. Finally note that the move we have chosen leaves d1 ≥ 0. Thus we can repeat the process and reduce X to zero in X moves.
We have proved that this procedure minimises the number of moves if we accept the restriction that we do not make any transfers between 1 and n. Thus the full algorithm is: calculate the effect of the transfers from 1 to n and from n to 1 on X. If either of these transfers reduces X by more than 1, then take the move with the larger reduction; otherwise, find a move as above which reduces X by 1; repeat.
© John Scholes
jscholes@kalva.demon.co.uk
11 Apr 2002