{"record":{"id":"6dd6689e97bcccfa","repo":"TheAlgorithms/Java","slug":"invalid-input-0-k-n-is-required","errorCode":null,"errorMessage":"Invalid input: 0 ≤ k ≤ n is required.","messagePattern":"Invalid input: 0 ≤ k ≤ n is required\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/backtracking/ArrayCombination.java","lineNumber":24,"sourceCode":"/**\n * This class provides methods to find all combinations of integers from 0 to n-1\n * of a specified length k using backtracking.\n */\npublic final class ArrayCombination {\n    private ArrayCombination() {\n    }\n\n    /**\n     * Generates all possible combinations of length k from the integers 0 to n-1.\n     *\n     * @param n The total number of elements (0 to n-1).\n     * @param k The desired length of each combination.\n     * @return A list containing all combinations of length k.\n     * @throws IllegalArgumentException if n or k are negative, or if k is greater than n.\n     */\n    public static List<List<Integer>> combination(int n, int k) {\n        if (k < 0 || k > n) {\n            throw new IllegalArgumentException(\"Invalid input: 0 ≤ k ≤ n is required.\");\n        }\n\n        List<List<Integer>> combinations = new ArrayList<>();\n        combine(combinations, new ArrayList<>(), 0, n, k);\n        return combinations;\n    }\n\n    /**\n     * A helper method that uses backtracking to find combinations.\n     *\n     * @param combinations The list to store all valid combinations found.\n     * @param current The current combination being built.\n     * @param start The starting index for the current recursion.\n     * @param n The total number of elements (0 to n-1).\n     * @param k The desired length of each combination.\n     */\n    private static void combine(List<List<Integer>> combinations, List<Integer> current, int start, int n, int k) {\n        // Base case: combination found","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/backtracking/ArrayCombination.java#L6-L42","documentation":"Thrown by ArrayCombination.combination(n, k) when k is negative or exceeds n. A combination of length k from n elements only makes sense when 0 <= k <= n; otherwise no valid combination exists and the request is contradictory. The check `k < 0 || k > n` enforces this contract; n itself is implicitly required to be non-negative because k > n would otherwise be ambiguous.","triggerScenarios":"Calling `combination(3, -1)`, `combination(3, 5)`, or `combination(-2, 1)`. Note that `k > n` covers the case where n is negative and k is positive, but `combination(-1, -1)` would NOT throw (k==n) and `combination(-2, -1)` would NOT throw (k<n) — a latent edge case if n can be negative.","commonSituations":"k and n swapped by mistake in the call; k computed as list size but the source list smaller than expected; off-by-one where n represents a count but is passed as a length-minus-one.","solutions":["Verify that 0 <= k <= n before calling, and that n is non-negative.","Double-check argument order — combination(n, k) takes total first, then subset size.","If k can exceed n legitimately in your flow, clamp k to n or return an empty result upstream."],"exampleFix":"// before\nArrayCombination.combination(3, 5);  // throws\n\n// after\nif (k < 0 || n < 0 || k > n) return Collections.emptyList();\nArrayCombination.combination(n, k);","handlingStrategy":"validation","validationCode":"public static boolean validCombinationArgs(int n, int k) {\n    return n >= 0 && k >= 0 && k <= n;\n}\n// usage\nif (!validCombinationArgs(n, k)) return Collections.emptyList();\nArrayCombination.combination(n, k);","typeGuard":"public static boolean validCombinationArgs(int n, int k) {\n    return n >= 0 && k >= 0 && k <= n;\n}","tryCatchPattern":"try {\n    return ArrayCombination.combination(n, k);\n} catch (IllegalArgumentException e) {\n    return Collections.emptyList();\n}","preventionTips":["Remember argument order: combination(n, k) — total first, subset size second.","Guard the call site if k or n can be negative.","Treat k > n as 'no combinations' rather than an exception where appropriate."],"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"}