TheAlgorithms/Java · error · IllegalArgumentException

Index must be non-negative

Error message

Index must be non-negative

What it means

Thrown by CatalanNumbers.catalan(int n) when n is negative. Catalan numbers C(n) are defined for n >= 0; a negative index has no mathematical meaning. Additionally, the computation calls factorial(2*n) which with a negative n would compute factorial of a negative number — the private factorial method would return 1 via its loop (since i starts at 2 > negative n), producing a wrong result rather than crashing. The guard prevents this silent incorrect behavior.

Source

Thrown at src/main/java/com/thealgorithms/maths/CatalanNumbers.java:18

package com.thealgorithms.maths;

/**
 * Calculate Catalan Numbers
 */
public final class CatalanNumbers {
    private CatalanNumbers() {
    }

    /**
     * Calculate the nth Catalan number using a recursive formula.
     *
     * @param n the index of the Catalan number to compute
     * @return the nth Catalan number
     */
    public static long catalan(final int n) {
        if (n < 0) {
            throw new IllegalArgumentException("Index must be non-negative");
        }
        return factorial(2 * n) / (factorial(n + 1) * factorial(n));
    }

    /**
     * Calculate the factorial of a number.
     *
     * @param n the number to compute the factorial for
     * @return the factorial of n
     */
    private static long factorial(final int n) {
        if (n == 0 || n == 1) {
            return 1;
        }
        long result = 1;
        for (int i = 2; i <= n; i++) {
            result *= i;
        }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate n >= 0 before calling CatalanNumbers.catalan(n).
  2. Review loop/recurrence boundaries where n is computed to ensure no underflow.
  3. Add range validation at the input boundary to reject negative indices early.

Example fix

// before
CatalanNumbers.catalan(-1); // throws 'Index must be non-negative'

// after
if (n >= 0) {
    long catalan = CatalanNumbers.catalan(n);
} else {
    throw new IllegalArgumentException("Catalan index must be >= 0, got: " + n);
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate n before calling CatalanNumbers.catalan
if (n < 0) {
    throw new IllegalArgumentException("Catalan index must be >= 0, got: " + n);
}
long catalan = CatalanNumbers.catalan(n);

Type guard

static boolean isValidCatalanIndex(int n) {
    return n >= 0;
}

Prevention

When it happens

Trigger: Calling CatalanNumbers.catalan(-1), CatalanNumbers.catalan(-5), or passing a computed index that is negative due to an arithmetic underflow (e.g., catalan(k - 2) where k < 2).

Common situations: An off-by-one error in a recurrence or loop. A user-supplied index from unvalidated input (CLI argument, API parameter). A subtraction in the index computation that produces a negative value without a prior range check.

Related errors


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