{"record":{"id":"3ef574912bf9f6a3","repo":"TheAlgorithms/JavaScript","slug":"the-sum-must-be-non-negative","errorCode":null,"errorMessage":"The sum must be non-negative.","messagePattern":"The sum must be non-negative\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Dynamic-Programming/NumberOfSubsetEqualToGivenSum.js","lineNumber":11,"sourceCode":"/*\nGiven an array of positive integers and a value sum,\ndetermine the total number of the subset with sum\nequal to the given sum.\n*/\n/*\n  Given solution is O(n*sum) Time complexity and O(sum) Space complexity\n*/\nfunction NumberOfSubsetSum(array, sum) {\n  if (sum < 0) {\n    throw new Error('The sum must be non-negative.')\n  }\n\n  if (!array.every((num) => num > 0)) {\n    throw new Error('All of the inputs of the array must be positive.')\n  }\n  const dp = [] // create an dp array where dp[i] denote number of subset with sum equal to i\n  for (let i = 1; i <= sum; i++) {\n    dp[i] = 0\n  }\n  dp[0] = 1 // since sum equal to 0 is always possible with no element in subset\n\n  for (let i = 0; i < array.length; i++) {\n    for (let j = sum; j >= array[i]; j--) {\n      if (j - array[i] >= 0) {\n        dp[j] += dp[j - array[i]]\n      }\n    }\n  }","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Dynamic-Programming/NumberOfSubsetEqualToGivenSum.js#L1-L29","documentation":"Thrown by NumberOfSubsetSum(array, sum) (plain Error) when sum < 0. The dynamic-programming table is indexed 0..sum, so a negative sum would construct an invalid range; the guard rejects it before allocation.","triggerScenarios":"NumberOfSubsetSum(arr, -5); passing a target derived from subtraction that went negative; defaulting sum to -1 as a 'no target' sentinel.","commonSituations":"Targets computed as (a - b) where b > a; user input parsed as negative; reused variable left at -1 from an initialization.","solutions":["Clamp the target to >= 0, or treat negative targets as a distinct 'no subsets' case returning 0.","Validate sum >= 0 at the call site before invoking.","Stop using -1 as a sentinel for 'unset'; use null/undefined and branch explicitly.","Recompute targets from fresh inputs after upstream subtractions."],"exampleFix":"// before\nconst count = NumberOfSubsetSum(arr, target) // throws if target < 0\n\n// after\nif (target < 0) return 0\nconst count = NumberOfSubsetSum(arr, target)","handlingStrategy":"validation","validationCode":"function safeSubsetSum(arr, sum) {\n  if (sum < 0) return 0 // or throw a domain-specific error\n  return NumberOfSubsetSum(arr, sum)\n}","typeGuard":"const isNonNegative = (n) => typeof n === 'number' && n >= 0","tryCatchPattern":"try {\n  return NumberOfSubsetSum(arr, sum)\n} catch (e) {\n  if (e instanceof Error && /non-negative/i.test(e.message)) return 0\n  throw e\n}","preventionTips":["Validate sum >= 0 before calling.","Don't use -1 as a sentinel for 'unset target'; use null and branch.","Clamp targets from subtraction to 0 when negative means 'impossible'.","Recompute targets from fresh inputs after upstream math."],"tags":["dynamic-programming","subset-sum","validation","non-negative"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}