Divided Apples is an interesting problem. If you find the editorial difficult to understand, have a look at my explanation below:
Transform a circle into all zeros
Let’s call a[i]
the number of apples the ith boy has. First, it is common sense to calculate the average of a
and subtract every a[i]
to this average. Our notation now has a new meaning: a[i]
is the number of apples the ith boy needs to give away. Keep in mind that a[i]
can be negative and a negative a[i]
means boy ith needs to receive abs(a[i])
from others. Also note that the sum of all a[i]
equals to 0. With this setup, the problem now becomes: “calculate the minimum number of moves to transform numbers in a circle into all zeros”.
9 ___ 6 2 __ -1
5 / \ 10 --> -2 / \ 3
\_____/ \____/
11 1 4 -6
Transform a line into all zeros
Let’s assume that the boy with 2 apples gives/receives nothing to/from the boy on his left (his previous boy). We can safely break this circle into a line without effecting the result:
2 __ -1
-2 / \ 3 --> 2 -1 3 -6 4 -2
\____/
4 -6
How to minimize the number of moves to transform a line 2 -1 3 -6 4 -2
into 0 0 0 0 0 0
? The answer is very straightforward: Starting from the 1st boy in line, each boy passes all apples he has in hand to the next person (the boy on his right):
- The 1st boy, in order to make 0, needs to give 2 apples to his right. After doing that, the line becomes
0 1 3 -6 4 -2
. The total number of moves increases by 2 apples. - The 2nd boy, in order to make 0, needs to give 1 apple to his right. After doing that, the line becomes
0 0 4 -6 4 -2
. The total number of moves increases by 1 apple. - The 3rd boy, in order to make 0, needs to give 4 apple to his right. After doing that, the line becomes
0 0 0 -2 4 -2
. The total number of moves increases by 4 apples. - The 4th boy, in order to make 0, needs to give -2 apple to his right (i.e. receive 2 apple from his right). After doing that, the line becomes
0 0 0 0 2 -2
. The total number of moves increases by 2 apples. - …
Do you see the pattern? The ith boy, in order to make 0, needs to give a[1]+a[2]+...+a[i]
apples to his right. After doing that, the total number of moves increases by |a[1]+a[2]+...+a[i]|
.
Summing up every boy’s number of moves, we get the following result: (1) The minimum number of moves to transform a line a
into all zeros is:
|a[1]| + |a[1]+a[2]| + |a[1]+a[2]+a[3]| + ... + |a[1]+a[2]+a[3]+...+a[n]|
.
Back to the circle problem
In the example above, we break the circle into a line by assuming that the 1st boy gives/receives nothing to/from his left. Now let’s reverse that assumption by allowing x
to be the number of apples the 1st boy receives from his left. This makes the number of apples the 1st boy need to give away increases from a[1]
to x+a[1]
. Replace a[1]
with x+a[1]
in (1) yields the following result: The minimum number of moves to transform a circle a
into all zeros given that the 1st boy receives x apples from his left is: f(x) = |x+a[1]| + |x+a[1]+a[2]| + |x+a[1]+a[2]+a[3]| + ... + |x+a[1]+a[2]+a[3]+...+a[n]|
.
Now what is left is to find x
that minimizes f(x)
.
Define b[i] = -(a[1]+a[2]+a[3]+ ... +a[i])
, we get f(x) = |x-b[1]| + |x-b[2]| + |x-b[3]| + ... + |x-b[n]|
. At this point, it should not be difficult to figure out that the optimal value for x
is the median of b
. If you are interested in the proof, check out this link: The median minimizes the sum of absolute deviations.