Given: $$ X = \left\{ x_1, x_2, \ldots , x_n \right\}\text{ with }x_i \in \mathbb N\text{ and }1 \le x_i \le x_{i+1} $$ $$ z \in \mathbb N $$
Wanted result: $$ Y = \left\{ y_1, y_2, \ldots , y_n \right\}\text{ with }y_i \in \mathbb N and 0 \le y_i \le y_{i+1}\text{ and }(x_i-y_i) \le (x_{i+1}-y_{i+1}) $$
Where: $$ \sum \limits_{i=0}^n (x_i-y_i) = z $$
For example:
$X = \{1, 5, 50\}$, $z = 8$
Then the result would be: $Y = \{0, 2, 46\}$ because $X - Y$ (component wise) $= \{1, 3, 4\}$. $1+3+4=8$.
Is there a efficient solution to this problem?
I have implemented a solution in javascript if anybody is interested:
This takes $X$ and $z$ as parameters and returns $X - Y$.
Here also a working example: http://jsfiddle.net/ujQh7/2/