{"record":{"id":"6a0f600011acaff2","repo":"TheAlgorithms/Java","slug":"input-array-should-not-contain-negative-number-s","errorCode":null,"errorMessage":"Input array should not contain negative number(s).","messagePattern":"Input array should not contain negative number\\(s\\)\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/dynamicprogramming/MinimumSumPartition.java","lineNumber":28,"sourceCode":"Input:  array[] = {1, 6, 11, 4}\nOutput: 0\nExplanation:\nSubset1 = {1, 4, 6}, sum of Subset1 = 11\nSubset2 = {11}, sum of Subset2 = 11\n\nInput:  array[] = {36, 7, 46, 40}\nOutput: 23\nExplanation:\nSubset1 = {7, 46} ;  sum of Subset1 = 53\nSubset2 = {36, 40} ; sum of Subset2  = 76\n */\npublic final class MinimumSumPartition {\n    private MinimumSumPartition() {\n    }\n\n    private static void throwIfInvalidInput(final int[] array) {\n        if (Arrays.stream(array).anyMatch(a -> a < 0)) {\n            throw new IllegalArgumentException(\"Input array should not contain negative number(s).\");\n        }\n    }\n\n    public static int minimumSumPartition(final int[] array) {\n        throwIfInvalidInput(array);\n        int sum = Arrays.stream(array).sum();\n        boolean[] dp = new boolean[sum / 2 + 1];\n        dp[0] = true; // Base case , don't select any element from array\n\n        // Find the closest sum of subset array that we can achieve which is closest to half of sum of full array\n        int closestPartitionSum = 0;\n\n        for (int i = 0; i < array.length; i++) {\n            for (int j = sum / 2; j > 0; j--) {\n                if (array[i] <= j) {\n                    dp[j] = dp[j] || dp[j - array[i]];\n                }\n                if (dp[j]) {","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/dynamicprogramming/MinimumSumPartition.java#L10-L46","documentation":"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).'","triggerScenarios":"Passing an array containing a negative value; reading signed integers from external data without sanitizing; using a sentinel like -1 in the input.","commonSituations":"Mixing sentinel/error markers into real data; off-by-one when slicing arrays; numeric parsing that allows leading '-' signs.","solutions":["Filter or reject negative values before calling minimumSumPartition (validate at the data boundary).","Convert the problem domain so weights are non-negative, or take absolute values only if semantically correct.","Add a unit test that asserts no negative slips through the input pipeline."],"exampleFix":"// before\nint result = MinimumSumPartition.minimumSumPartition(arr);\n\n// after\nfor (int v : arr) {\n    if (v < 0) throw new IllegalArgumentException(\"no negatives: \" + v);\n}\nint result = MinimumSumPartition.minimumSumPartition(arr);","handlingStrategy":"validation","validationCode":"for (int v : array) {\n    if (v < 0) throw new IllegalArgumentException(\"no negatives: \" + v);\n}\nMinimumSumPartition.minimumSumPartition(array);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate numeric ranges at the data-ingest boundary.","Avoid mixing sentinel values (-1) into payload arrays.","Add property-based tests asserting non-negative inputs."],"tags":["input-validation","array","dynamic-programming","negative-values"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}