Splitting a number between N numbers to make them have as little difference between them as possible Say I have the numbers 2, 3, 0 and I want to split the number 4 between them with the goal of making them as equal as possible.
The result in the above case would be: 3, 3, 3. We give 1 to the 2 and 3 to the 0 and now we're used up our 4 and made the numbers as equal as possible.
If we had 2, 3, 0 and split 5 between them, then the result would be 3.3, 3.3, 3.3.
If we had 2, 3, 0 and 1 to split, then the result would be 2, 3, 1.
If we had 5, 7 and 4 to split, then the result would be 8, 8.
etc.
I need this for a game I'm making where there's a grid and a liquid spreads equally to the grid tiles. I tried playing around with division and ratios but I'm in the dark to be honest.
 A: Say you start with $x,y,z$ and the number you add is $a$. After you add $a$, the total liquid will be $x+y+z+a$, and the average liquid per tile is $(x+y+z+a)/3$. Your examples seem to indicate that you can't remove liquid from any tile, only add, so you can achieve an even distribution of $(x+y+z+a)/3$ per tile provided that $(x+y+z+a)/3 \geq \max(x,y,z)$.
Now if $(x+y+z+a)/3<\max(x,y,z)$, then we can't get an even distribution because (at least) one of the tiles already has too much liquid. So $z=\max(x,y,z)$ (just reorder them if that's not the case). Then we don't want to add anything to $z$ since it already exceeds the average $(x+y+z+a)/3$. So we will try to make $x$ and $y$ as even as possible. The total liquid in those two tiles after adding $a$ will be $x+y+a$ and the average is $(x+y+a)/2$. We can achieve an even split if $(x+y+a)/2 \geq \max(x,y)$. 
Otherwise, $(x+y+a)/2 <\max(x,y)$, so one of the two remaining tiles already has too much liquid. Say that $y=\max(x,y)$ (again, reorder them if that's not the case). Then we don't want to add anything to $y$, so we just add all the liquid to $x$.
To sum up: say the amounts are $x,y,z$ where $x \leq y \leq z$, and we add the amount $a$.


*

*If $(x+y+z+a)/3 \geq \max(x,y,z)=z$, we can evenly distribute the liquid to end up with $(x+y+z+a)/3$ in each tile.

*If $(x+y+z+a)/3< \max(x,y,z)=z$ but $(x+y+a)/2 \geq \max(x,y)=y$, then we add nothing to $z$ and we evenly distribute the liquid between $x$ and $y$ to get $(x+y+a)/2$ in each.

*If $(x+y+z+a)/3< \max(x,y,z)=z$ and $(x+y+a)/2 < \max(x,y)=y$, then we add all the liquid to $x$ to get $x+a$, and we leave $y$ and $z$ alone.

A: I mean, a brute force algorithm is to keep an array of all numbers, and always increase the lowest number by 1 until there's nothing left to increase. Is this too slow for your application?
A faster way is to sort all numbers and starting with 2nd smallest calculate how much total will need to be added to all numbers smaller than this one to make all of them equal. Once you reach the number for which the sum is larger than the number you need to distribute, you can fill in.
Example
1 2 3 4 5, need to distribute 4
^--------
2 2 3 4 5, need to distribute 3
--^------
3 3 3 4 5, need to distribute 1
----^----
4 3 3 4 5, need to distribute 0
Done
