Excellent explanation, this helped me a lot, thankyou so much!!.

I read through the entire post and i could deduce the expression step by step :

if all elements are equal, say all elements are represented by min, then sum of all elements is :

len*min = sum

Now, say all elements are not equal, in that case, we keep adding 1 to each element, until it reaches max value.

say for ex : [1, 2, 3, 4]

step 1: 1 + 1 ( first elem)

step 2 : 2 + 1( first elem + 1)

step 3..... so on

hence left side expression becomes

len*(min + k) = sum + k * (len - 1)

lenmin + klen = sum + klen - k

lenmin = sum - k

k = sum - len*min