-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
subsetsum.go
56 lines (45 loc) · 1.26 KB
/
subsetsum.go
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
//Given a set of non-negative integers, and a (positive) value sum,
//determine if there is a subset of the given set with sum
//equal to given sum.
// time complexity: O(n*sum)
// space complexity: O(n*sum)
//references: https://www.geeksforgeeks.org/subset-sum-problem-dp-25/
package dynamic
import "fmt"
var ErrInvalidPosition = fmt.Errorf("invalid position in subset")
var ErrNegativeSum = fmt.Errorf("negative sum is not allowed")
func IsSubsetSum(array []int, sum int) (bool, error) {
if sum < 0 {
//not allow negative sum
return false, ErrNegativeSum
}
//create subset matrix
arraySize := len(array)
subset := make([][]bool, arraySize+1)
for i := 0; i <= arraySize; i++ {
subset[i] = make([]bool, sum+1)
}
for i := 0; i <= arraySize; i++ {
//sum 0 is always true
subset[i][0] = true
}
for i := 1; i <= sum; i++ {
//empty set is false when sum is not 0
subset[0][i] = false
}
for i := 1; i <= arraySize; i++ {
for j := 1; j <= sum; j++ {
if array[i-1] > j {
subset[i][j] = subset[i-1][j]
}
if array[i-1] <= j {
if j-array[i-1] < 0 || j-array[i-1] > sum {
//out of bounds
return false, ErrInvalidPosition
}
subset[i][j] = subset[i-1][j] || subset[i-1][j-array[i-1]]
}
}
}
return subset[arraySize][sum], nil
}