{"record":{"id":"e86add494be9eacf","repo":"TheAlgorithms/Java","slug":"input-must-be-non-negative-received","errorCode":null,"errorMessage":"Input must be non-negative. Received: ","messagePattern":"Input must be non-negative\\. Received: ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/LeonardoNumber.java","lineNumber":39,"sourceCode":"    }\n\n    /**\n     * Calculates the nth Leonardo Number using recursion.\n     * <p>\n     * Time Complexity: O(2^n) - exponential due to repeated calculations\n     * Space Complexity: O(n) - due to recursion stack\n     * <p>\n     * Note: This method is not recommended for large values of n due to exponential\n     * time complexity.\n     * Consider using {@link #leonardoNumberIterative(int)} for better performance.\n     *\n     * @param n the index of the Leonardo Number to calculate (must be non-negative)\n     * @return the nth Leonardo Number\n     * @throws IllegalArgumentException if n is negative\n     */\n    public static int leonardoNumber(int n) {\n        if (n < 0) {\n            throw new IllegalArgumentException(\"Input must be non-negative. Received: \" + n);\n        }\n        if (n == 0 || n == 1) {\n            return 1;\n        }\n        return leonardoNumber(n - 1) + leonardoNumber(n - 2) + 1;\n    }\n\n    /**\n     * Calculates the nth Leonardo Number using an iterative approach.\n     * <p>\n     * This method provides better performance than the recursive version for large\n     * values of n.\n     * <p>\n     * Time Complexity: O(n)\n     * Space Complexity: O(1)\n     *\n     * @param n the index of the Leonardo Number to calculate (must be non-negative)\n     * @return the nth Leonardo Number","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/LeonardoNumber.java#L21-L57","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Validate that n >= 0 before calling leonardoNumber","Use the iterative variant leonardoNumberIterative for any production use","Clamp computed indices to a minimum of 0"],"exampleFix":"// before\nint result = LeonardoNumber.leonardoNumber(index);\n\n// after\nif (index < 0) {\n    throw new IllegalArgumentException(\"Index must be non-negative: \" + index);\n}\nint result = LeonardoNumber.leonardoNumberIterative(index);","handlingStrategy":"validation","validationCode":"if (n < 0) {\n    throw new IllegalArgumentException(\"Index must be non-negative: \" + n);\n}\nint result = LeonardoNumber.leonardoNumber(n);","typeGuard":"static boolean isValidLeonardoIndex(int n) {\n    return n >= 0;\n}","tryCatchPattern":null,"preventionTips":["Prefer the iterative leonardoNumberIterative over the recursive variant for performance","Clamp computed indices to a minimum of 0 before calling","Validate index arithmetic results that could underflow"],"tags":["math","validation","illegal-argument","recursion"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}