TheAlgorithms/Java · error · IllegalArgumentException

Invalid input: 0 ≤ k ≤ n is required.

Error message

Invalid input: 0 ≤ k ≤ n is required.

What it means

Thrown by ArrayCombination.combination(n, k) when k is negative or exceeds n. A combination of length k from n elements only makes sense when 0 <= k <= n; otherwise no valid combination exists and the request is contradictory. The check `k < 0 || k > n` enforces this contract; n itself is implicitly required to be non-negative because k > n would otherwise be ambiguous.

Source

Thrown at src/main/java/com/thealgorithms/backtracking/ArrayCombination.java:24

/**
 * This class provides methods to find all combinations of integers from 0 to n-1
 * of a specified length k using backtracking.
 */
public final class ArrayCombination {
    private ArrayCombination() {
    }

    /**
     * Generates all possible combinations of length k from the integers 0 to n-1.
     *
     * @param n The total number of elements (0 to n-1).
     * @param k The desired length of each combination.
     * @return A list containing all combinations of length k.
     * @throws IllegalArgumentException if n or k are negative, or if k is greater than n.
     */
    public static List<List<Integer>> combination(int n, int k) {
        if (k < 0 || k > n) {
            throw new IllegalArgumentException("Invalid input: 0 ≤ k ≤ n is required.");
        }

        List<List<Integer>> combinations = new ArrayList<>();
        combine(combinations, new ArrayList<>(), 0, n, k);
        return combinations;
    }

    /**
     * A helper method that uses backtracking to find combinations.
     *
     * @param combinations The list to store all valid combinations found.
     * @param current The current combination being built.
     * @param start The starting index for the current recursion.
     * @param n The total number of elements (0 to n-1).
     * @param k The desired length of each combination.
     */
    private static void combine(List<List<Integer>> combinations, List<Integer> current, int start, int n, int k) {
        // Base case: combination found

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Verify that 0 <= k <= n before calling, and that n is non-negative.
  2. Double-check argument order — combination(n, k) takes total first, then subset size.
  3. If k can exceed n legitimately in your flow, clamp k to n or return an empty result upstream.

Example fix

// before
ArrayCombination.combination(3, 5);  // throws

// after
if (k < 0 || n < 0 || k > n) return Collections.emptyList();
ArrayCombination.combination(n, k);
Defensive patterns

Strategy: validation

Validate before calling

public static boolean validCombinationArgs(int n, int k) {
    return n >= 0 && k >= 0 && k <= n;
}
// usage
if (!validCombinationArgs(n, k)) return Collections.emptyList();
ArrayCombination.combination(n, k);

Type guard

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

Try / catch

try {
    return ArrayCombination.combination(n, k);
} catch (IllegalArgumentException e) {
    return Collections.emptyList();
}

Prevention

When it happens

Trigger: Calling `combination(3, -1)`, `combination(3, 5)`, or `combination(-2, 1)`. Note that `k > n` covers the case where n is negative and k is positive, but `combination(-1, -1)` would NOT throw (k==n) and `combination(-2, -1)` would NOT throw (k<n) — a latent edge case if n can be negative.

Common situations: k and n swapped by mistake in the call; k computed as list size but the source list smaller than expected; off-by-one where n represents a count but is passed as a length-minus-one.

Related errors


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