I need help with array sum algorithm, sum of result array values is never greater then limit, I have provided input => output bellow
I have tried something like this but It doesn't work properly for all tests in example
input
.map((x, i) => input
.slice(0, i + 1)
.reduce((prev, cur) => prev + cur, 0) > limit ? Math.max((x - (input.reduce((prev, cur) => prev + cur, 0) - limit)), 0) : x)
Example tests:
limit = 500
[0] => [0]
[300] => [300]
[600] => [500]
[0,1000] => [0, 500],
[600,300] => [500,0]
[500,0,0] => [500,0,0]
[400,200,0] => [400,100,0]
[0,200,300] => [0,200,300]
[0,600,300] => [0,500,0]
[0,0,600] => [0,0,500]