TheAlgorithms/Java · error · IllegalArgumentException

Input must be non-negative. Received:

Error message

Input must be non-negative. Received: 

What it means

Thrown by the recursive leonardoNumber(int n) when n is negative. Leonardo numbers follow L(0)=1, L(1)=1, L(n)=L(n-1)+L(n-2)+1. A negative index has no definition in this recurrence, and the recursion would never reach its base case.

Source

Thrown at src/main/java/com/thealgorithms/maths/LeonardoNumber.java:39

    }

    /**
     * Calculates the nth Leonardo Number using recursion.
     * <p>
     * Time Complexity: O(2^n) - exponential due to repeated calculations
     * Space Complexity: O(n) - due to recursion stack
     * <p>
     * Note: This method is not recommended for large values of n due to exponential
     * time complexity.
     * Consider using {@link #leonardoNumberIterative(int)} for better performance.
     *
     * @param n the index of the Leonardo Number to calculate (must be non-negative)
     * @return the nth Leonardo Number
     * @throws IllegalArgumentException if n is negative
     */
    public static int leonardoNumber(int n) {
        if (n < 0) {
            throw new IllegalArgumentException("Input must be non-negative. Received: " + n);
        }
        if (n == 0 || n == 1) {
            return 1;
        }
        return leonardoNumber(n - 1) + leonardoNumber(n - 2) + 1;
    }

    /**
     * Calculates the nth Leonardo Number using an iterative approach.
     * <p>
     * This method provides better performance than the recursive version for large
     * values of n.
     * <p>
     * Time Complexity: O(n)
     * Space Complexity: O(1)
     *
     * @param n the index of the Leonardo Number to calculate (must be non-negative)
     * @return the nth Leonardo Number

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate that n >= 0 before calling leonardoNumber
  2. Use the iterative variant leonardoNumberIterative for any production use
  3. Clamp computed indices to a minimum of 0

Example fix

// before
int result = LeonardoNumber.leonardoNumber(index);

// after
if (index < 0) {
    throw new IllegalArgumentException("Index must be non-negative: " + index);
}
int result = LeonardoNumber.leonardoNumberIterative(index);
Defensive patterns

Strategy: validation

Validate before calling

if (n < 0) {
    throw new IllegalArgumentException("Index must be non-negative: " + n);
}
int result = LeonardoNumber.leonardoNumber(n);

Type guard

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

Prevention

When it happens

Trigger: Calling leonardoNumber(-1) or any negative index. Also hit when n is computed from an expression that can underflow (e.g., leonardoNumber(start - offset) where offset > start).

Common situations: Index arithmetic that can produce negative values. User input parsed directly without bounds checking. Note: this recursive version is O(2^n); passing large positive values will be extremely slow, though that produces a timeout rather than this error.

Related errors


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