forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SumOfSubset.js
64 lines (54 loc) · 2.09 KB
/
SumOfSubset.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
*
* Sum of Subset problem
*
* Given an ordered set W of non-negative integers and a value K,
* determine all possible subsets from the given set W whose sum
* of its elements equals to the given value K.
*
* More info: https://www.geeksforgeeks.org/subset-sum-backtracking-4/
*/
/*
* @param {number[]} set Original set of numbers
* @param {number[]} subset Subset being evaluated
* @param {number} setIndex Index from set of last element in subset
* @param {number} Sum of elements from subset
* @param {targetSum} The target sum on which the subset sum is compared to
* @returns {number[][]} Subsets whose elements add up to targetSum
*/
const sumOfSubset = (set, subset, setindex, sum, targetSum) => {
// Base case where the subset sum is equal to target sum
// Evaluation of following subsets on this path will always add up to
// greater than targetSum, so no need to continue
if (sum === targetSum) return [subset]
// This and following subsets on this path will always add up to
// greater than targetSum, so no need to continue
if (sum > targetSum) return []
// Initialize results array. Will contain only valid subsets
let results = []
// Slice gets from the set all the elements at the right of the last element
// to be evaluated (last element of subset)
// forEach iterated on the resulting array
set.slice(setindex).forEach((num, index) => {
// The next subset to be evaluated, current subset plus next element
const nextSubset = [...subset, num]
// Next index from the set. Current set index plus iteration index
// index starts at 0, so a + 1 is required
const nextSetIndex = setindex + index + 1
// Sum of elements from the next subset to be evaluated
const nextSum = sum + num
// Call recursively the sumOfSubset for the nextSubset
const subsetResult = sumOfSubset(
set,
nextSubset,
nextSetIndex,
nextSum,
targetSum
)
// Concat the recursive result with current result array
results = [...results, ...subsetResult]
})
// Return results
return results
}
export { sumOfSubset }