Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function subsetSum(a, n) {
- if (a.length === 0) return false
- const el = a[0]
- if (el === n) return [ el ]
- const rest = a.slice(1)
- const resultWithEl = subsetSum(rest, n - el)
- if (resultWithEl) return resultWithEl.concat([ el ])
- const resultWithoutEl = subsetSum(rest, n)
- if (resultWithoutEl) return resultWithoutEl
- return false
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement