TheAlgorithms/Java · error · IllegalArgumentException

n can't be smaller than k

Error message

n can't be smaller than k

What it means

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.

Source

Thrown at src/main/java/com/thealgorithms/maths/Combinations.java:52

    /**
     * The above method can exceed limit of long (overflow) when factorial(n) is
     * larger than limits of long variable. Thus even if nCk is within range of
     * long variable above reason can lead to incorrect result. This is an
     * optimized version of computing combinations. Observations: nC(k + 1) = (n
     * - k) * nCk / (k + 1) We know the value of nCk when k = 1 which is nCk = n
     * Using this base value and above formula we can compute the next term
     * nC(k+1)
     *
     * @param n
     * @param k
     * @return nCk
     */
    public static long combinationsOptimized(int n, int k) {
        if (n < 0 || k < 0) {
            throw new IllegalArgumentException("n or k can't be negative");
        }
        if (n < k) {
            throw new IllegalArgumentException("n can't be smaller than k");
        }
        // nC0 is always 1
        long solution = 1;
        for (int i = 0; i < k; i++) {
            solution = (n - i) * solution / (i + 1);
        }
        return solution;
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure n >= k before calling, or return 0 if k > n is a valid 'impossible selection' in your domain.
  2. Check for swapped arguments: verify which parameter is the population and which is the selection.
  3. Bound k at the call site: k = Math.min(k, n).

Example fix

// before
long c = Combinations.combinationsOptimized(2, 5); // throws

// after
long c = (5 > 2) ? 0 : Combinations.combinationsOptimized(2, 5);
// or fix the order:
long c = Combinations.combinationsOptimized(5, 2);
Defensive patterns

Strategy: validation

Validate before calling

if (n < k) {
    // decide: invalid, or empty selection returning 0
    return 0; // or throw a domain-specific error
}
Combinations.combinationsOptimized(n, k);

Type guard

static boolean isValidChoice(int n, int k) { return n >= 0 && k >= 0 && n >= k; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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