{"record":{"id":"390978f616d1bc74","repo":"TheAlgorithms/JavaScript","slug":"all-of-the-inputs-of-the-array-must-be-positive","errorCode":null,"errorMessage":"All of the inputs of the array must be positive.","messagePattern":"All of the inputs of the array must be positive\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Dynamic-Programming/NumberOfSubsetEqualToGivenSum.js","lineNumber":15,"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  }\n  return dp[sum]\n}\n\nexport { NumberOfSubsetSum }","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Dynamic-Programming/NumberOfSubsetEqualToGivenSum.js#L1-L33","documentation":"Thrown by NumberOfSubsetSum(array, sum) (plain Error) when array contains any element that is not strictly greater than 0 (i.e. zeros and negatives are rejected). The DP assumes positive integers because it uses element values as index deltas; zero/negative elements would break the inner loop's j >= array[i] bound or loop forever.","triggerScenarios":"NumberOfSubsetSum([1, 0, 2], 3); NumberOfSubsetSum([5, -1, 2], 4); an array containing a zero-valued placeholder.","commonSituations":"Datasets with zero-initialized or nullable entries; signed measurements; arrays that include sentinel zeros for 'no data'.","solutions":["Filter out non-positive elements before calling: arr.filter((n) => n > 0).","Validate at the boundary: if (!arr.every((n) => Number.isInteger(n) && n > 0)) reject.","Replace zero/negative sentinels with actual positive values or exclude those records.","If negatives are legitimate in your domain, use a different algorithm (the classic subset-sum with offset)."],"exampleFix":"// before\nconst c = NumberOfSubsetSum(arr, target) // throws if arr has 0 or negatives\n\n// after\nconst positives = arr.filter((n) => n > 0)\nconst c = NumberOfSubsetSum(positives, target)","handlingStrategy":"validation","validationCode":"function safeSubsetSum(arr, sum) {\n  const positives = arr.filter((n) => Number.isInteger(n) && n > 0)\n  return NumberOfSubsetSum(positives, sum)\n}","typeGuard":"const allPositiveInts = (arr) =>\n  Array.isArray(arr) && arr.every((n) => Number.isInteger(n) && n > 0)","tryCatchPattern":"try {\n  return NumberOfSubsetSum(arr, sum)\n} catch (e) {\n  if (e instanceof Error && /must be positive/i.test(e.message)) {\n    return NumberOfSubsetSum(arr.filter((n) => n > 0), sum)\n  }\n  throw e\n}","preventionTips":["Filter out zeros and negatives before calling.","Validate every element is a positive integer at the boundary.","Replace zero-sentinel placeholders with real values or drop those records.","Switch to an offset-based subset-sum if negatives are legitimately part of your domain."],"tags":["dynamic-programming","subset-sum","validation","positive-integers"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}