{"record":{"id":"10ffd5ac3fba1b63","repo":"TheAlgorithms/Go","slug":"errinvalidposition","errorCode":"ErrInvalidPosition","errorMessage":"invalid position in subset","messagePattern":"invalid position in subset","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"dynamic/subsetsum.go","lineNumber":12,"sourceCode":"//Given a set of non-negative integers, and a (positive) value sum,\n//determine if there is a subset of the given set with sum\n//equal to given sum.\n// time complexity: O(n*sum)\n// space complexity: O(n*sum)\n//references: https://www.geeksforgeeks.org/subset-sum-problem-dp-25/\n\npackage dynamic\n\nimport \"fmt\"\n\nvar ErrInvalidPosition = fmt.Errorf(\"invalid position in subset\")\nvar ErrNegativeSum = fmt.Errorf(\"negative sum is not allowed\")\n\nfunc IsSubsetSum(array []int, sum int) (bool, error) {\n\tif sum < 0 {\n\t\t//not allow negative sum\n\t\treturn false, ErrNegativeSum\n\t}\n\n\t//create subset matrix\n\tarraySize := len(array)\n\tsubset := make([][]bool, arraySize+1)\n\tfor i := 0; i <= arraySize; i++ {\n\t\tsubset[i] = make([]bool, sum+1)\n\t}\n\n\tfor i := 0; i <= arraySize; i++ {\n\t\t//sum 0 is always true\n\t\tsubset[i][0] = true","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/dynamic/subsetsum.go#L1-L30","documentation":"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.","triggerScenarios":"Thrown at dynamic/subsetsum.go:12 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","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"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}