{"record":{"id":"b6cdea98184e6af9","repo":"TheAlgorithms/Java","slug":"variable-number-must-be-between-1-and-numberofvar","errorCode":null,"errorMessage":"Variable number must be between 1 and {numberOfVariables}","messagePattern":"Variable number must be between 1 and (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/graphs/TwoSat.java","lineNumber":129,"sourceCode":"     * Adds a clause of the form (a ∨ b) to the boolean expression.\n     *\n     * <p>\n     * Example: To add (¬x₁ ∨ x₂), call:\n     * </p>\n     *\n     * <pre>{@code\n     * addClause(1, true, 2, false);\n     * }</pre>\n     *\n     * @param a         the first variable (1 ≤ a ≤ numberOfVariables)\n     * @param isNegateA {@code true} if variable {@code a} is negated\n     * @param b         the second variable (1 ≤ b ≤ numberOfVariables)\n     * @param isNegateB {@code true} if variable {@code b} is negated\n     * @throws IllegalArgumentException if {@code a} or {@code b} are out of range\n     */\n    void addClause(int a, boolean isNegateA, int b, boolean isNegateB) {\n        if (a <= 0 || a > numberOfVariables) {\n            throw new IllegalArgumentException(\"Variable number must be between 1 and \" + numberOfVariables);\n        }\n        if (b <= 0 || b > numberOfVariables) {\n            throw new IllegalArgumentException(\"Variable number must be between 1 and \" + numberOfVariables);\n        }\n\n        a = isNegateA ? negate(a) : a;\n        b = isNegateB ? negate(b) : b;\n        int notA = negate(a);\n        int notB = negate(b);\n\n        // Add implications: (¬a → b) and (¬b → a)\n        graph[notA].add(b);\n        graph[notB].add(a);\n\n        // Build transpose graph\n        graphTranspose[b].add(notA);\n        graphTranspose[a].add(notB);\n    }","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/graphs/TwoSat.java#L111-L147","documentation":"Thrown by `TwoSat.addClause` when the first variable `a` is `<= 0` or `> numberOfVariables`. TwoSat variables are 1-indexed (x1..xn), so a valid variable must be in `[1, numberOfVariables]`. This guards array indexing in the implication graph before adding the clause's implications.","triggerScenarios":"Calling `addClause(a, isNegateA, b, isNegateB)` where `a` is 0, negative, or larger than the count passed to the constructor (e.g. adding a clause with variable x6 to a `new TwoSat(5)`).","commonSituations":"0-based vs 1-based confusion; variable count set too small; clauses referencing a variable that does not exist.","solutions":["Ensure `1 <= a <= numberOfVariables` for the first argument","If your variables are 0-based, add 1 before calling addClause","Increase the constructor's variable count if the variable is legitimately new"],"exampleFix":"// before (0-based passed to a 1-based API)\nts.addClause(0, false, 2, false);\n// after (1-based)\nts.addClause(1, false, 2, false);","handlingStrategy":"validation","validationCode":"if (a <= 0 || a > numberOfVariables) {\n    throw new IllegalArgumentException(\"a must be in [1, \" + numberOfVariables + \"]\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    ts.addClause(a, na, b, nb);\n} catch (IllegalArgumentException e) {\n    // handle out-of-range variable\n}","preventionTips":["Remember TwoSat variables are 1-indexed","Validate every clause variable against the constructor count"],"tags":["sat","indexing","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}