{"record":{"id":"6b21a089042c5834","repo":"TheAlgorithms/Java","slug":"the-combination-length-cannot-be-negative","errorCode":null,"errorMessage":"The combination length cannot be negative.","messagePattern":"The combination length cannot be negative\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/backtracking/Combination.java","lineNumber":25,"sourceCode":"import java.util.TreeSet;\n\n/**\n * Finds all combinations of a given array using backtracking algorithm * @author Alan Piao (<a href=\"https://github.com/cpiao3\">git-Alan Piao</a>)\n */\npublic final class Combination {\n    private Combination() {\n    }\n\n    /**\n     * Find all combinations of given array using backtracking\n     * @param arr the array.\n     * @param n length of combination\n     * @param <T> the type of elements in the array.\n     * @return a list of all combinations of length n. If n == 0, return null.\n     */\n    public static <T> List<TreeSet<T>> combination(T[] arr, int n) {\n        if (n < 0) {\n            throw new IllegalArgumentException(\"The combination length cannot be negative.\");\n        }\n\n        if (n == 0) {\n            return Collections.emptyList();\n        }\n        T[] array = arr.clone();\n        Arrays.sort(array);\n\n        List<TreeSet<T>> result = new LinkedList<>();\n        backtracking(array, n, 0, new TreeSet<T>(), result);\n        return result;\n    }\n\n    /**\n     * Backtrack all possible combinations of a given array\n     * @param arr the array.\n     * @param n length of the combination\n     * @param index the starting index.","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/backtracking/Combination.java#L7-L43","documentation":"Thrown by Combination.combination(arr, n) when the requested combination length n is negative. A combination cannot have a negative number of elements; the method returns an empty list for n == 0 but rejects any n < 0. Note: the javadoc states n==0 returns null but the code actually returns Collections.emptyList().","triggerScenarios":"Calling `combination(arr, -1)` or `combination(arr, -5)`. The guard `n < 0` rejects negatives; `n == 0` is valid and returns an empty list.","commonSituations":"n derived from a subtraction that can go negative (e.g. targetLength - offset); n read from user input defaulting to -1 as a sentinel; n computed from a size that was expected to be larger.","solutions":["Ensure n >= 0; treat n == 0 as 'no combinations' rather than passing a negative sentinel.","Clamp n to 0 if it can legitimately be 0, or guard the call site with `if (n < 0) return ...`.","Check the upstream calculation that produced n for off-by-one or underflow."],"exampleFix":"// before\nCombination.combination(arr, len - offset);  // throws if offset > len\n\n// after\nint n = Math.max(0, len - offset);\nif (n == 0) return Collections.emptyList();\nCombination.combination(arr, n);","handlingStrategy":"validation","validationCode":"if (n < 0) return Collections.emptyList();\nCombination.combination(arr, n);","typeGuard":"public static boolean validCombinationLength(int n) {\n    return n >= 0;\n}","tryCatchPattern":"try {\n    return Combination.combination(arr, n);\n} catch (IllegalArgumentException e) {\n    return Collections.emptyList();\n}","preventionTips":["Clamp n to 0 if it can underflow.","Guard upstream subtractions that produce n.","Treat n == 0 as valid (returns empty list), not as an error."],"tags":["backtracking","combinatorics","argument-validation","illegalargumentexception"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}