TheAlgorithms/Java · error · IllegalArgumentException

Variable number must be between 1 and {numberOfVariables}

Error message

Variable number must be between 1 and {numberOfVariables}

What it means

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.

Source

Thrown at src/main/java/com/thealgorithms/datastructures/graphs/TwoSat.java:129

     * Adds a clause of the form (a ∨ b) to the boolean expression.
     *
     * <p>
     * Example: To add (¬x₁ ∨ x₂), call:
     * </p>
     *
     * <pre>{@code
     * addClause(1, true, 2, false);
     * }</pre>
     *
     * @param a         the first variable (1 ≤ a ≤ numberOfVariables)
     * @param isNegateA {@code true} if variable {@code a} is negated
     * @param b         the second variable (1 ≤ b ≤ numberOfVariables)
     * @param isNegateB {@code true} if variable {@code b} is negated
     * @throws IllegalArgumentException if {@code a} or {@code b} are out of range
     */
    void addClause(int a, boolean isNegateA, int b, boolean isNegateB) {
        if (a <= 0 || a > numberOfVariables) {
            throw new IllegalArgumentException("Variable number must be between 1 and " + numberOfVariables);
        }
        if (b <= 0 || b > numberOfVariables) {
            throw new IllegalArgumentException("Variable number must be between 1 and " + numberOfVariables);
        }

        a = isNegateA ? negate(a) : a;
        b = isNegateB ? negate(b) : b;
        int notA = negate(a);
        int notB = negate(b);

        // Add implications: (¬a → b) and (¬b → a)
        graph[notA].add(b);
        graph[notB].add(a);

        // Build transpose graph
        graphTranspose[b].add(notA);
        graphTranspose[a].add(notB);
    }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure `1 <= a <= numberOfVariables` for the first argument
  2. If your variables are 0-based, add 1 before calling addClause
  3. Increase the constructor's variable count if the variable is legitimately new

Example fix

// before (0-based passed to a 1-based API)
ts.addClause(0, false, 2, false);
// after (1-based)
ts.addClause(1, false, 2, false);
Defensive patterns

Strategy: validation

Validate before calling

if (a <= 0 || a > numberOfVariables) {
    throw new IllegalArgumentException("a must be in [1, " + numberOfVariables + "]");
}

Try / catch

try {
    ts.addClause(a, na, b, nb);
} catch (IllegalArgumentException e) {
    // handle out-of-range variable
}

Prevention

When it happens

Trigger: 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)`).

Common situations: 0-based vs 1-based confusion; variable count set too small; clauses referencing a variable that does not exist.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/b6cdea98184e6af9. Report an issue: GitHub.