MathLabs

Problem 5

nn people are seated in a circle. A total of nknk coins are distributed among them, not necessarily equally. A move transfers one coin between two adjacent people. Find an algorithm using the minimum number of moves that leaves everyone with the same number of coins.
Step 4 of 5: Handle the zero-prefix case
dj≥0,d1+⋯+dj−1<0d_j\ge0,\qquad d_1+\cdots+d_{j-1}<0
Detailed analysis

In the zero-prefix case, choose the first j>i+1j>i+1 with dj≥0d_j\ge0. Such a jj exists because the total deviation is zero. Transfer one coin from jj to j−1j-1; the negative prefix through j−1j-1 moves one unit toward zero, so XX decreases by one. The chosen move keeps d1≥0d_1\ge0.