{"record":{"id":"b1c5e111d620865c","repo":"TheAlgorithms/Java","slug":"number-is-negative","errorCode":null,"errorMessage":"number is negative","messagePattern":"number is negative","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/Combinations.java","lineNumber":18,"sourceCode":"package com.thealgorithms.maths;\n\n/**\n * @see <a href=\"https://en.wikipedia.org/wiki/Combination\">Combination</a>\n */\npublic final class Combinations {\n    private Combinations() {\n    }\n\n    /**\n     * Calculate of factorial\n     *\n     * @param n the number\n     * @return factorial of given number\n     */\n    public static long factorial(int n) {\n        if (n < 0) {\n            throw new IllegalArgumentException(\"number is negative\");\n        }\n        return n == 0 || n == 1 ? 1 : n * factorial(n - 1);\n    }\n\n    /**\n     * Calculate combinations\n     *\n     * @param n first number\n     * @param k second number\n     * @return combinations of given {@code n} and {@code k}\n     */\n    public static long combinations(int n, int k) {\n        return factorial(n) / (factorial(k) * factorial(n - k));\n    }\n\n    /**\n     * The above method can exceed limit of long (overflow) when factorial(n) is\n     * larger than limits of long variable. Thus even if nCk is within range of","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/Combinations.java#L1-L36","documentation":"Thrown by Combinations.factorial when n < 0. Factorial is defined only for non-negative integers; the recursive implementation (n * factorial(n-1)) would recurse indefinitely for negative n since decrementing never reaches the 0/1 base case. The guard converts an infinite recursion into a clear, immediate error.","triggerScenarios":"Calling factorial(-1) or any negative argument. Common when n is computed from a subtraction (e.g. factorial(a - b)) that can go negative, or when reading an unvalidated value.","commonSituations":"Arithmetic that derives n from user inputs which can underflow below zero; permutation/combination scaffolding where the order of subtraction is reversed; test data that includes 0 and negatives without filtering.","solutions":["Pass a non-negative integer n (>= 0).","Guard the caller: if (n < 0) throw or clamp.","Prefer Combinations.combinations(n, k) which encapsulates factorial internally, rather than calling factorial directly with derived values."],"exampleFix":"// before\nlong f = Combinations.factorial(count - 1); // count == 0 => -1\n\n// after\nlong f = Combinations.factorial(Math.max(0, count - 1));","handlingStrategy":"validation","validationCode":"if (n < 0) {\n    throw new IllegalArgumentException(\"factorial requires n >= 0, got \" + n);\n}\nCombinations.factorial(n);","typeGuard":"static boolean isNonNegative(int n) { return n >= 0; }","tryCatchPattern":null,"preventionTips":["Guard derived factorials like factorial(a - b) with Math.max(0, a - b).","Prefer the higher-level combinations() API over direct factorial calls.","Validate parsed integers before passing to factorial."],"tags":["validation","number-theory","precondition","recursion"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}