TheAlgorithms/Java · error · IllegalArgumentException
n or k can't be negative
Error message
n or k can't be negative
What it means
Thrown by Combinations.combinationsOptimized when n < 0 or k < 0. Binomial coefficients C(n,k) are defined for non-negative n and k; negative values have no combinatorial meaning. The guard fires before the n < k check, so it specifically catches negative inputs as distinct from the ordering error.
Source
Thrown at src/main/java/com/thealgorithms/maths/Combinations.java:49
return factorial(n) / (factorial(k) * factorial(n - k));
}
/**
* 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
- Pass non-negative n and k (>= 0).
- Validate both at the caller: if (n < 0 || k < 0) reject before calling.
- Clamp derived values with Math.max(0, ...) where a negative result indicates an empty selection.
Example fix
// before long c = Combinations.combinationsOptimized(total - picked, spare); // after long c = Combinations.combinationsOptimized(Math.max(0, total - picked), Math.max(0, spare));
Defensive patterns
Strategy: validation
Validate before calling
if (n < 0 || k < 0) {
throw new IllegalArgumentException("n and k must be >= 0");
}
Combinations.combinationsOptimized(n, k); Type guard
static boolean areNonNegative(int n, int k) { return n >= 0 && k >= 0; } Prevention
- Bound k against n and against zero before calling.
- Use Math.max(0, ...) on values derived from subtraction.
- Validate UI inputs that accept signed integers.
When it happens
Trigger: Calling combinationsOptimized(-1, 2), combinationsOptimized(5, -1), or both negative. Triggered when either parameter is derived from subtraction that can underflow, or parsed from unvalidated input.
Common situations: n or k computed as differences that can go negative; UI inputs accepting negative numbers; passing k larger than expected and then subtracting to a negative index.
Related errors
- n can't be smaller than k
- Table is empty; cannot find keys.
- Numbers array cannot be empty or null
- Number must be positive.
- Given range of values is invalid!
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/ac17bd732814adcc.
Report an issue: GitHub.