TheAlgorithms/JavaScript · error · Error

All of the inputs of the array must be positive.

Error message

All of the inputs of the array must be positive.

What it means

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.

Source

Thrown at Dynamic-Programming/NumberOfSubsetEqualToGivenSum.js:15

/*
Given an array of positive integers and a value sum,
determine the total number of the subset with sum
equal to the given sum.
*/
/*
  Given solution is O(n*sum) Time complexity and O(sum) Space complexity
*/
function NumberOfSubsetSum(array, sum) {
  if (sum < 0) {
    throw new Error('The sum must be non-negative.')
  }

  if (!array.every((num) => num > 0)) {
    throw new Error('All of the inputs of the array must be positive.')
  }
  const dp = [] // create an dp array where dp[i] denote number of subset with sum equal to i
  for (let i = 1; i <= sum; i++) {
    dp[i] = 0
  }
  dp[0] = 1 // since sum equal to 0 is always possible with no element in subset

  for (let i = 0; i < array.length; i++) {
    for (let j = sum; j >= array[i]; j--) {
      if (j - array[i] >= 0) {
        dp[j] += dp[j - array[i]]
      }
    }
  }
  return dp[sum]
}

export { NumberOfSubsetSum }

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Filter out non-positive elements before calling: arr.filter((n) => n > 0).
  2. Validate at the boundary: if (!arr.every((n) => Number.isInteger(n) && n > 0)) reject.
  3. Replace zero/negative sentinels with actual positive values or exclude those records.
  4. If negatives are legitimate in your domain, use a different algorithm (the classic subset-sum with offset).

Example fix

// before
const c = NumberOfSubsetSum(arr, target) // throws if arr has 0 or negatives

// after
const positives = arr.filter((n) => n > 0)
const c = NumberOfSubsetSum(positives, target)
Defensive patterns

Strategy: validation

Validate before calling

function safeSubsetSum(arr, sum) {
  const positives = arr.filter((n) => Number.isInteger(n) && n > 0)
  return NumberOfSubsetSum(positives, sum)
}

Type guard

const allPositiveInts = (arr) =>
  Array.isArray(arr) && arr.every((n) => Number.isInteger(n) && n > 0)

Try / catch

try {
  return NumberOfSubsetSum(arr, sum)
} catch (e) {
  if (e instanceof Error && /must be positive/i.test(e.message)) {
    return NumberOfSubsetSum(arr.filter((n) => n > 0), sum)
  }
  throw e
}

Prevention

When it happens

Trigger: NumberOfSubsetSum([1, 0, 2], 3); NumberOfSubsetSum([5, -1, 2], 4); an array containing a zero-valued placeholder.

Common situations: Datasets with zero-initialized or nullable entries; signed measurements; arrays that include sentinel zeros for 'no data'.

Related errors


AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13). Data as JSON: /api/errors/390978f616d1bc74. Report an issue: GitHub.