This applies to any language, but I'd be implementing it in node.
I have a set of integers, and a value that I need to subtract from the sum of the set.
[4, 5, 6, 7, 8] - 25
If we subtract from every number evenly, we'd get:
[-1, 0, 1, 2, 3]
However, I don't want any numbers less than 0. So, if I were writing an algorithm to do this, the negative numbers would overflow equally into the remaining numbers, and we'd now have:
[0, 0, 1, 2, 3] - 1
Making the resulting set:
[0, 0, 1 - 0.333, 2 - 0.333, 3 - 0.333]
Is there any fast operation or series of operations that don't involve looping until completion to solve this problem?
Note that this is exactly the outcome I want. All of the negative values overflow EVENLY into the remaining positive values.
I use Lodash so an answer in Lodash would be acceptable.