{"record":{"id":"85442a0802452824","repo":"TheAlgorithms/Java","slug":"n-must-be-non-negative","errorCode":null,"errorMessage":"n must be non-negative","messagePattern":"n must be non-negative","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/BellNumbers.java","lineNumber":31,"sourceCode":" *\n * @author Chahat Sandhu, <a href=\"https://github.com/singhc7\">singhc7</a>\n * @see <a href=\"https://en.wikipedia.org/wiki/Bell_number\">Bell Number (Wikipedia)</a>\n */\npublic final class BellNumbers {\n\n    private BellNumbers() {\n    }\n\n    /**\n     * Calculates the n-th Bell number using the Bell Triangle.\n     *\n     * @param n the index of the Bell number (must be non-negative)\n     * @return the n-th Bell number\n     * @throws IllegalArgumentException if n is negative or n > 25\n     */\n    public static long compute(int n) {\n        if (n < 0) {\n            throw new IllegalArgumentException(\"n must be non-negative\");\n        }\n        if (n == 0) {\n            return 1;\n        }\n        if (n > 25) {\n            throw new IllegalArgumentException(\"n must be <= 25. For larger n, use BigInteger implementation.\");\n        }\n\n        // We use a 2D array to visualize the Bell Triangle\n        long[][] bellTriangle = new long[n + 1][n + 1];\n\n        // Base case: The triangle starts with 1\n        bellTriangle[0][0] = 1;\n\n        for (int i = 1; i <= n; i++) {\n            // Rule 1: The first number in a new row is the LAST number of the previous row\n            bellTriangle[i][0] = bellTriangle[i - 1][i - 1];\n","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/BellNumbers.java#L13-L49","documentation":"Thrown by BellNumbers.compute(int n) when n is negative. Bell numbers B(n) are defined for n >= 0; a negative index has no combinatorial meaning. The method uses a Bell Triangle stored in a 2D array of size (n+1) x (n+1), so a negative n would cause a NegativeArraySizeException if not guarded — this check prevents that.","triggerScenarios":"Calling BellNumbers.compute(-1), BellNumbers.compute(-100), or passing a computed index that underflows to negative (e.g., compute(someValue - offset) where offset > someValue).","commonSituations":"An off-by-one error in a loop boundary (e.g., iterating from n-1 down to 0 but calling compute(i-1)). A user-supplied index parsed from input without range validation. A subtraction that produces a negative result passed directly as n.","solutions":["Validate n >= 0 before calling BellNumbers.compute(n).","Check loop boundaries and arithmetic that computes n to ensure it cannot go negative.","Add input validation at the data boundary to reject negative indices before they reach the computation."],"exampleFix":"// before\nBellNumbers.compute(-1); // throws 'n must be non-negative'\n\n// after\nif (n >= 0) {\n    long bell = BellNumbers.compute(n);\n} else {\n    throw new IllegalArgumentException(\"Bell number index must be >= 0, got: \" + n);\n}","handlingStrategy":"validation","validationCode":"// Validate n before calling BellNumbers.compute\nif (n < 0) {\n    throw new IllegalArgumentException(\"Bell number index must be >= 0, got: \" + n);\n}\nif (n > 25) {\n    // use BigInteger implementation instead\n    throw new UnsupportedOperationException(\"Use BigInteger implementation for n > 25\");\n}\nlong bell = BellNumbers.compute(n);","typeGuard":"static boolean isValidBellIndex(int n) {\n    return n >= 0 && n <= 25;\n}","tryCatchPattern":null,"preventionTips":["Validate n >= 0 AND n <= 25 in a single guard before calling BellNumbers.compute.","Check loop/recurrence boundaries to prevent index underflow.","Cap user-supplied indices at the input boundary."],"tags":["combinatorics","input-validation","bell-numbers","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}