{"record":{"id":"aea89199267608c3","repo":"TheAlgorithms/Go","slug":"errnegativesum","errorCode":"ErrNegativeSum","errorMessage":"negative sum is not allowed","messagePattern":"negative sum is not allowed","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"dynamic/subsetsum.go","lineNumber":13,"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\n\t}","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/dynamic/subsetsum.go#L1-L31","documentation":"A sentinel error (dynamic.ErrNegativeSum) returned by IsSubsetSum when the requested sum is negative; the subset-sum DP assumes non-negative integers and a non-negative target, so a negative sum is rejected by this generic guard before the matrix is built.","triggerScenarios":"Thrown at dynamic/subsetsum.go:13 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Validate sum>=0 before calling","Return false for negative sums at the call site if that matches your semantics","Compare with errors.Is(err, ErrNegativeSum) for specific handling"],"exampleFix":null,"handlingStrategy":"validation","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"}