TheAlgorithms/Go · error
ErrInvalidPosition
ErrInvalidPosition
Error message
invalid position in subset
What it means
Inside the subset-sum DP loop, index j-array[i-1] would fall outside the subset table (negative or beyond sum) for the current element, so IsSubsetSum bails out with ErrInvalidPosition instead of indexing out of bounds — typically caused by array elements larger than the target sum.
Source
Thrown at dynamic/subsetsum.go:12
//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] = trueView on GitHub (pinned to 5ba447ec5f)
Solutions
- Filter array elements greater than sum before calling IsSubsetSum
- Guard the index computation (j-array[i-1]) before table access
- Handle via errors.Is(err, ErrInvalidPosition) and sanitize the input set
Defensive patterns
Strategy: type-guard
When it happens
Trigger: Thrown at dynamic/subsetsum.go:12 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02).
Data as JSON: /api/errors/10ffd5ac3fba1b63.
Report an issue: GitHub.