TheAlgorithms/Java · error · IllegalArgumentException

Input array should not contain negative number(s).

Error message

Input array should not contain negative number(s).

What it means

Thrown by MinimumSumPartition.minimumSumPartition(int[]) when any array element is negative. The algorithm builds a subset-sum DP table indexed by sums; negative values would create invalid/negative array indices, so they are rejected up front via throwIfInvalidInput. The message is 'Input array should not contain negative number(s).'

Solutions

  1. Filter or reject negative values before calling minimumSumPartition (validate at the data boundary).
  2. Convert the problem domain so weights are non-negative, or take absolute values only if semantically correct.
  3. Add a unit test that asserts no negative slips through the input pipeline.

Example fix

// before
int result = MinimumSumPartition.minimumSumPartition(arr);

// after
for (int v : arr) {
    if (v < 0) throw new IllegalArgumentException("no negatives: " + v);
}
int result = MinimumSumPartition.minimumSumPartition(arr);
Defensive patterns

Strategy: validation

Validate before calling

for (int v : array) {
    if (v < 0) throw new IllegalArgumentException("no negatives: " + v);
}
MinimumSumPartition.minimumSumPartition(array);

Prevention

When it happens

Trigger: Passing an array containing a negative value; reading signed integers from external data without sanitizing; using a sentinel like -1 in the input.

Common situations: Mixing sentinel/error markers into real data; off-by-one when slicing arrays; numeric parsing that allows leading '-' signs.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/6a0f600011acaff2. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/com/thealgorithms/dynamicprogramming/MinimumSumPartition.java:28

Input:  array[] = {1, 6, 11, 4}
Output: 0
Explanation:
Subset1 = {1, 4, 6}, sum of Subset1 = 11
Subset2 = {11}, sum of Subset2 = 11

Input:  array[] = {36, 7, 46, 40}
Output: 23
Explanation:
Subset1 = {7, 46} ;  sum of Subset1 = 53
Subset2 = {36, 40} ; sum of Subset2  = 76
 */
public final class MinimumSumPartition {
    private MinimumSumPartition() {
    }

    private static void throwIfInvalidInput(final int[] array) {
        if (Arrays.stream(array).anyMatch(a -> a < 0)) {
            throw new IllegalArgumentException("Input array should not contain negative number(s).");
        }
    }

    public static int minimumSumPartition(final int[] array) {
        throwIfInvalidInput(array);
        int sum = Arrays.stream(array).sum();
        boolean[] dp = new boolean[sum / 2 + 1];
        dp[0] = true; // Base case , don't select any element from array

        // Find the closest sum of subset array that we can achieve which is closest to half of sum of full array
        int closestPartitionSum = 0;

        for (int i = 0; i < array.length; i++) {
            for (int j = sum / 2; j > 0; j--) {
                if (array[i] <= j) {
                    dp[j] = dp[j] || dp[j - array[i]];
                }
                if (dp[j]) {

View on GitHub (pinned to fdfb9a395b)