TheAlgorithms/Java · error · IllegalArgumentException

The combination length cannot be negative.

Error message

The combination length cannot be negative.

What it means

Thrown by Combination.combination(arr, n) when the requested combination length n is negative. A combination cannot have a negative number of elements; the method returns an empty list for n == 0 but rejects any n < 0. Note: the javadoc states n==0 returns null but the code actually returns Collections.emptyList().

Source

Thrown at src/main/java/com/thealgorithms/backtracking/Combination.java:25

import java.util.TreeSet;

/**
 * Finds all combinations of a given array using backtracking algorithm * @author Alan Piao (<a href="https://github.com/cpiao3">git-Alan Piao</a>)
 */
public final class Combination {
    private Combination() {
    }

    /**
     * Find all combinations of given array using backtracking
     * @param arr the array.
     * @param n length of combination
     * @param <T> the type of elements in the array.
     * @return a list of all combinations of length n. If n == 0, return null.
     */
    public static <T> List<TreeSet<T>> combination(T[] arr, int n) {
        if (n < 0) {
            throw new IllegalArgumentException("The combination length cannot be negative.");
        }

        if (n == 0) {
            return Collections.emptyList();
        }
        T[] array = arr.clone();
        Arrays.sort(array);

        List<TreeSet<T>> result = new LinkedList<>();
        backtracking(array, n, 0, new TreeSet<T>(), result);
        return result;
    }

    /**
     * Backtrack all possible combinations of a given array
     * @param arr the array.
     * @param n length of the combination
     * @param index the starting index.

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure n >= 0; treat n == 0 as 'no combinations' rather than passing a negative sentinel.
  2. Clamp n to 0 if it can legitimately be 0, or guard the call site with `if (n < 0) return ...`.
  3. Check the upstream calculation that produced n for off-by-one or underflow.

Example fix

// before
Combination.combination(arr, len - offset);  // throws if offset > len

// after
int n = Math.max(0, len - offset);
if (n == 0) return Collections.emptyList();
Combination.combination(arr, n);
Defensive patterns

Strategy: validation

Validate before calling

if (n < 0) return Collections.emptyList();
Combination.combination(arr, n);

Type guard

public static boolean validCombinationLength(int n) {
    return n >= 0;
}

Try / catch

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

Prevention

When it happens

Trigger: Calling `combination(arr, -1)` or `combination(arr, -5)`. The guard `n < 0` rejects negatives; `n == 0` is valid and returns an empty list.

Common situations: n derived from a subtraction that can go negative (e.g. targetLength - offset); n read from user input defaulting to -1 as a sentinel; n computed from a size that was expected to be larger.

Related errors


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