{"record":{"id":"8e225d2a71a7bd43","repo":"TheAlgorithms/Java","slug":"the-number-of-pairs-of-parentheses-cannot-be-negat","errorCode":null,"errorMessage":"The number of pairs of parentheses cannot be negative","messagePattern":"The number of pairs of parentheses cannot be negative","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/backtracking/ParenthesesGenerator.java","lineNumber":22,"sourceCode":"import java.util.List;\n\n/**\n * This class generates all valid combinations of parentheses for a given number of pairs using backtracking.\n */\npublic final class ParenthesesGenerator {\n    private ParenthesesGenerator() {\n    }\n\n    /**\n     * Generates all valid combinations of parentheses for a given number of pairs.\n     *\n     * @param n The number of pairs of parentheses.\n     * @return A list of strings representing valid combinations of parentheses.\n     * @throws IllegalArgumentException if n is less than 0.\n     */\n    public static List<String> generateParentheses(final int n) {\n        if (n < 0) {\n            throw new IllegalArgumentException(\"The number of pairs of parentheses cannot be negative\");\n        }\n        List<String> result = new ArrayList<>();\n        generateParenthesesHelper(result, \"\", 0, 0, n);\n        return result;\n    }\n\n    /**\n     * Helper function for generating all valid combinations of parentheses recursively.\n     *\n     * @param result  The list to store valid combinations.\n     * @param current The current combination being formed.\n     * @param open    The number of open parentheses.\n     * @param close   The number of closed parentheses.\n     * @param n       The total number of pairs of parentheses.\n     */\n    private static void generateParenthesesHelper(List<String> result, final String current, final int open, final int close, final int n) {\n        if (current.length() == n * 2) {\n            result.add(current);","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/backtracking/ParenthesesGenerator.java#L4-L40","documentation":"Thrown by ParenthesesGenerator.generateParentheses(n) when n is negative. The generator enumerates all well-formed strings of n pairs of parentheses, which requires n >= 0. The generator accepts n == 0 (returns a list containing the empty string via the helper) and rejects any negative count.","triggerScenarios":"Calling `generateParentheses(-1)` or `generateParentheses(-3)`. The guard `n < 0` rejects negatives; n == 0 is valid.","commonSituations":"n read from input that defaults to -1 when missing; n derived from a subtraction that underflows; passing a count before validating the source data.","solutions":["Ensure n >= 0; if n == 0 is a valid 'generate nothing' case, handle it explicitly rather than passing a negative.","Validate/parse input and reject negative values before calling.","Clamp n to 0 if a non-negative fallback is acceptable."],"exampleFix":"// before\nParenthesesGenerator.generateParentheses(parsedN);  // throws if parsedN < 0\n\n// after\nint n = Math.max(0, parsedN);\nParenthesesGenerator.generateParentheses(n);","handlingStrategy":"validation","validationCode":"if (n < 0) throw new IllegalArgumentException(\"n must be >= 0\");\nParenthesesGenerator.generateParentheses(n);","typeGuard":"public static boolean validPairCount(int n) {\n    return n >= 0;\n}","tryCatchPattern":"try {\n    return ParenthesesGenerator.generateParentheses(n);\n} catch (IllegalArgumentException e) {\n    return Collections.emptyList();\n}","preventionTips":["Parse/validate n from input before calling.","Clamp to 0 if a non-negative fallback is acceptable.","Avoid using -1 as a sentinel value."],"tags":["backtracking","string-generation","argument-validation","illegalargumentexception"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}