{"record":{"id":"5866b76a0cebe3b2","repo":"TheAlgorithms/Java","slug":"n-can-t-be-smaller-than-k","errorCode":null,"errorMessage":"n can't be smaller than k","messagePattern":"n can't be smaller than k","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/Combinations.java","lineNumber":52,"sourceCode":"    /**\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\n     * long variable above reason can lead to incorrect result. This is an\n     * optimized version of computing combinations. Observations: nC(k + 1) = (n\n     * - k) * nCk / (k + 1) We know the value of nCk when k = 1 which is nCk = n\n     * Using this base value and above formula we can compute the next term\n     * nC(k+1)\n     *\n     * @param n\n     * @param k\n     * @return nCk\n     */\n    public static long combinationsOptimized(int n, int k) {\n        if (n < 0 || k < 0) {\n            throw new IllegalArgumentException(\"n or k can't be negative\");\n        }\n        if (n < k) {\n            throw new IllegalArgumentException(\"n can't be smaller than k\");\n        }\n        // nC0 is always 1\n        long solution = 1;\n        for (int i = 0; i < k; i++) {\n            solution = (n - i) * solution / (i + 1);\n        }\n        return solution;\n    }\n}\n","sourceCodeStart":34,"sourceCodeEnd":62,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/Combinations.java#L34-L62","documentation":"Thrown by Combinations.combinationsOptimized when n < k (after the negativity guard passed). The number of ways to choose k items from n is zero when k exceeds n in the standard definition, but this implementation treats it as an invalid argument rather than returning 0. The guard fires after the n<0||k<0 check, so reaching it means both are non-negative but inverted.","triggerScenarios":"Calling combinationsOptimized(2, 5) where the choose-count k exceeds the available n. Common when n and k are passed in the wrong order, or when k is computed independently and overshoots n.","commonSituations":"Swapped arguments (passing k, n instead of n, k); a selection size k that is not bounded by the population n; off-by-one where n was decremented but k was not.","solutions":["Ensure n >= k before calling, or return 0 if k > n is a valid 'impossible selection' in your domain.","Check for swapped arguments: verify which parameter is the population and which is the selection.","Bound k at the call site: k = Math.min(k, n)."],"exampleFix":"// before\nlong c = Combinations.combinationsOptimized(2, 5); // throws\n\n// after\nlong c = (5 > 2) ? 0 : Combinations.combinationsOptimized(2, 5);\n// or fix the order:\nlong c = Combinations.combinationsOptimized(5, 2);","handlingStrategy":"validation","validationCode":"if (n < k) {\n    // decide: invalid, or empty selection returning 0\n    return 0; // or throw a domain-specific error\n}\nCombinations.combinationsOptimized(n, k);","typeGuard":"static boolean isValidChoice(int n, int k) { return n >= 0 && k >= 0 && n >= k; }","tryCatchPattern":null,"preventionTips":["Double-check argument order: the method takes (n, k) = (population, selection).","Return 0 for k > n if that matches your combinatorial semantics instead of calling the method.","Bound k = Math.min(k, n) when overshoot is possible."],"tags":["validation","combinatorics","argument-order","precondition"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}