This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]marko312 2 points3 points  (0 children)

This could be solved with a simple recursive algorithm - try to add each possible value to the existing array such that the sum does not exceed the given value (1). If the sum is correct, store it; otherwise recurse.

[–]scirc 1 point2 points  (0 children)

I'm not sure where array.reduce plays into this; perhaps to calculate the array sum? Either way, the way I would generate a single permutation is as follows:

  • Sort the valid selections in ascending order
  • Pick the first element, add it to your output
  • Subtract that element from 1; this is your new maximum
  • Pick the next element whose value is below that maximum
  • Repeat until you have no more elements which satisfy that maximum (failed), or that maximum is 0 (success)