TheAlgorithms/Java · error · IllegalArgumentException

Start must be less than or equal to end. Given start: {start

Error message

Start must be less than or equal to end. Given start: {start}, end: {end}

What it means

Thrown by KaprekarNumbers.kaprekarNumberInRange(long start, long end) when start is strictly greater than end. The method iterates from start to end (inclusive) collecting Kaprekar numbers, so an inverted range would produce an empty result and is treated as a usage error. This is the first of two validation checks; it runs before the non-negative check (error 419).

Source

Thrown at src/main/java/com/thealgorithms/maths/KaprekarNumbers.java:45

 *      - Wikipedia</a>
 * @author TheAlgorithms (https://github.com/TheAlgorithms)
 */
public final class KaprekarNumbers {
    private KaprekarNumbers() {
    }

    /**
     * Finds all Kaprekar numbers within a given range (inclusive).
     *
     * @param start the starting number of the range (inclusive)
     * @param end   the ending number of the range (inclusive)
     * @return a list of all Kaprekar numbers in the specified range
     * @throws IllegalArgumentException if start is greater than end or if start is
     *                                  negative
     */
    public static List<Long> kaprekarNumberInRange(long start, long end) {
        if (start > end) {
            throw new IllegalArgumentException("Start must be less than or equal to end. Given start: " + start + ", end: " + end);
        }
        if (start < 0) {
            throw new IllegalArgumentException("Start must be non-negative. Given start: " + start);
        }

        ArrayList<Long> list = new ArrayList<>();
        for (long i = start; i <= end; i++) {
            if (isKaprekarNumber(i)) {
                list.add(i);
            }
        }

        return list;
    }

    /**
     * Checks whether a given number is a Kaprekar number.
     * <p>

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure start <= end before calling kaprekarNumberInRange().
  2. Swap start and end if the caller cannot guarantee ordering: if (start > end) { long t = start; start = end; end = t; }.
  3. Validate range bounds at the input/configuration boundary.

Example fix

// before
List<Long> result = KaprekarNumbers.kaprekarNumberInRange(from, to);

// after
long lo = Math.min(from, to);
long hi = Math.max(from, to);
if (lo < 0) {
    throw new IllegalArgumentException("Range must be non-negative: " + lo);
}
List<Long> result = KaprekarNumbers.kaprekarNumberInRange(lo, hi);
Defensive patterns

Strategy: validation

Validate before calling

if (start > end) {
    throw new IllegalArgumentException("start must be <= end: " + start + " > " + end);
}
List<Long> result = KaprekarNumbers.kaprekarNumberInRange(start, end);

Type guard

static boolean isValidRange(long start, long end) {
    return start <= end;
}

Try / catch

try {
    List<Long> result = KaprekarNumbers.kaprekarNumberInRange(start, end);
} catch (IllegalArgumentException e) {
    if (e.getMessage().contains("less than or equal")) {
        // start > end; swap or reject
    }
}

Prevention

When it happens

Trigger: Calling kaprekarNumberInRange(100, 10), kaprekarNumberInRange(50, 49), or any case where start > end. Note: equal values (start == end) are valid and do NOT trigger this error — only strictly greater does.

Common situations: Range parameters from user input where start/end may be swapped. Computed ranges from sorting or filtering logic that occasionally invert. Configuration with min/max fields filled in wrong order.

Related errors


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