CHEFMOVR - Editorial



Author: Dmytro Berezin
Primary Tester: Prateek Gupta
Editorialist: Hussain Kara Fallah


Given an array consisting of n integers, Chef wants to make all elements of this array equal. He can apply the following operation:

Pick 2 numbers

(i,j) :: j = i + D

Decrementing A_i OR A_j by 1 and adding 1 to the other element. Please help chef by telling the minimum number of operations he needs to make all elements equal, or tell that such situation is impossible.





Applying our operation any number of times would keep the sum of elements in our array the same. (Since we are subtracting one from an arbitrary number and adding it to another one), so the sum of our numbers must be divisible by n

The final value of each element would be equal to \frac{sum}{n}

In any operation chef would choose 2 numbers (i,j):

(i,j) :: j = i + D

j mod D = i mod D

So you can notice that any pair of elements Ai , Aj such that j mod D ≠ i mod D are independent from each other. (That’s true).

That means that we should group our elements by the remainder of their indexes after dividing by D. (And of course) solve each one independently. In fact we will have D groups, the ith group (0 ≤ i < D) contains all elements Aj (j mod D = i)

Let’s tell now about handling each set, applying our operation any number of times would keep the sum of elements in our set the same. So the sum of elements in each set must be divisible by this set’s size and of course the result of this division must be equal to \frac{sum}{n} , having one set violating this condition would make Chef’s mission impossible.

Let’s now move to finding the minimum number of operations to fix each set, each set would have 3 kinds of numbers (numbers > \frac{sum}{n}) and (numbers < \frac{sum}{n}) and of course (numbers equal to \frac{sum}{n}) -that we can omit-.

Let’s process numbers of each set in the same order of the array (from left to right) and separate them into 2 groups as described (first two types since we can omit the third). Maintaining 2 pointers each one iterating on the elements of one group, would just do the job, because if our processed element is less than \frac{sum}{n} then optimal choice would be picking this number along with the closest number bigger than \frac{sum}{n} to the right of it (the same when the opposite happens), and we should add the distance between them for each increment\decrement operation we apply (because we are using elements between them as mediators). After each iteration, one of the numbers referred to by out pointers would reach the desired value, so we move the pointer on. Practically, this part can be done in simpler way (like author’s solution), but this is the detailed explanation. The implementation of this editorial can be found in my code.


AUTHOR’s solution: Can be found here
TESTER’s solution: Can be found here
EDITORIALIST’s solution: Can be found here

I solved this problem easier.

First I determined the target value (as explained in editorial).

Go through the array right to left. At each index, we take as many units from the left corresponding array index (if there is one) as is necessary to bring the value to the target.

If the resulting array is equal, we output it. Otherwise, no solution exists.


  1. [3, 2, 5, 1, 4], size of mover = 1
  2. [3, 2, 5, 2, 3]
  3. [3, 2, 4, 3, 3]
  4. [3, 3, 3, 3, 3]
  5. [3, 3, 3, 3, 3].

Resulting array is equal, so we output it.

The problem PRLADDU set by me is a subproblem of this problem. Its editorial is really nicely written and explains a nice way of proving the greedy approach of this solution.

is there anybody who has an easy solution for this question .
I can only understand this solution upto this line

In any operation chef would choose 2 numbers (i,j):

After that I cannot understand what is the solution trying to tell . somewhere its saying j mod d = I mod d
then saying j mod d is not equal to d ??? I cannot understand this after the line above.

please help . thanks in advance

@geforce what i did was ,

  1. simply find out the average(avg) of all numbers in the array , and then run a loop from 0-(n-d) in the array. let the loop variable be i then make a[i] the avg value , and if the avg value is less then a[i] then substract it and make a[i]=avg and make a[i+d] + or - the sme number required to make a[i] the avg. at end check if all numbers are avg , if yes then yes else no
    you can refer to this

try out some examples of different size and different D.
you will notice that for some values there will be a range of elements which are not accessible. if all values are same after operations then possibly all element were accessible. see my code for understanding

Candy3 from spoj is also a subproblem of this problem.

Can anyone please explain to me why there’s this condition in Author’s solution? Thanks

       if (curSum % cnt != 0) {
				ans = -1;

As in the editorial reduce the problem to D independent subproblems - one for each subset based on residue$\mod D$.

Let a_1, a_2, ... a_k be such a subset. A move operation is performed on two adjacent elements in this subset. Let \bar a be the target (mean) value. Consider partial sums s_i=\sum_{j=1}^i a_j. Each s_i for i < k has to be brought to the target value i \bar a. s_k must be at the target value, otherwise the mission is unachievable. Observe that a move between a_i and a_{i+1} affects exactly one partial sum - s_i, and it changes it by 1. So for each 1 \le i \le k - 1 we need at least |s_i - i \bar a| = |\sum_{j=1}^i a_j - i\bar a|=|\sum_{j=1}^i (a_j - \bar a)| moves to bring s_i to its target value.

Thus we have the following lower bound for the number of moves:

\sum_{i=1}^{k-1} \left| \sum_{j=1}^i (a_j - \bar a)\right|

It’s easy to show that as long as the condition for s_k is satisfied, this bound is always achievable. At each iteration let a_i, i < k be the first element not equal to \bar a. If there is no such element - we are done. If a_i > \bar a perform a move operation to a_{i+1}. Otherwise, let j > i be the first such that a_j > 0 (it must exist). Perform a move operation to a_{j-1}. Since at each iteration a partial sum gets closer to its target value by 1, the total number of moves will be equal to the bound.

Here is the



I am doing the same thing as AUTHOR’s solution in C, but still I am getting WA in Task 3 of Sub-Task 2.
int main(void)
long long int t,n,d,i,sum,di,a[100015],f,c,j,s,ans;
return 0;
Can you please see where am I getting it wrong?

The link to solution wrong.