TheAlgorithms/Java · error · IllegalArgumentException
Input array must have length of at least two
Error message
Input array must have length of at least two
What it means
Thrown by SecondMinMax.findSecondMin / findSecondMax (via private checkInput) when the supplied int[] has fewer than 2 elements. A second-smallest or second-largest value is mathematically undefined for a 0- or 1-element array, so the method refuses to return a meaningless sentinel.
Source
Thrown at src/main/java/com/thealgorithms/maths/SecondMinMax.java:51
/**
* @brief Finds the Second minimum / maximum value from the array
* @param arr the input array
* @exception IllegalArgumentException => if input array is of length less than 2 also if all elements are same
* @return the second minimum / maximum value from the input array
* @author Bharath Sanjeevi ( https://github.com/BharathSanjeeviT )
*/
public static int findSecondMin(final int[] arr) {
return secondBest(arr, Integer.MAX_VALUE, (a, b) -> a < b);
}
public static int findSecondMax(final int[] arr) {
return secondBest(arr, Integer.MIN_VALUE, (a, b) -> a > b);
}
private static void checkInput(final int[] arr) {
if (arr.length < 2) {
throw new IllegalArgumentException("Input array must have length of at least two");
}
}
private static void checkOutput(final int secNum, final int initialVal) {
if (secNum == initialVal) {
throw new IllegalArgumentException("Input array should have at least 2 distinct elements");
}
}
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Guard the call site: only invoke findSecondMin/findSecondMax when arr.length >= 2.
- Fix the upstream data source so it actually supplies >= 2 elements.
- Return an Optional<Integer> from a wrapper if 'no second value' is a legitimate business state.
Example fix
// before
int second = SecondMinMax.findSecondMin(arr);
// after
if (arr == null || arr.length < 2) {
throw new IllegalArgumentException("Need at least 2 elements, got " + (arr == null ? 0 : arr.length));
}
int second = SecondMinMax.findSecondMin(arr); Defensive patterns
Strategy: validation
Validate before calling
if (arr == null || arr.length < 2) {
throw new IllegalArgumentException("Need >= 2 elements");
}
int s = SecondMinMax.findSecondMin(arr); Try / catch
try {
int s = SecondMinMax.findSecondMin(arr);
} catch (IllegalArgumentException e) {
return OptionalInt.empty();
} Prevention
- Always pair a length check with any 'second-best' statistic.
- Prefer returning OptionalInt from a wrapper when 'no second value' is a valid business state.
When it happens
Trigger: Call findSecondMin(new int[]{5}), findSecondMax(new int[]{}), or pass an array whose length was derived from an empty collection via stream/mapToInt without a size check.
Common situations: Processing a list that the caller assumed had multiple entries (e.g., results of a filter that returned one match), deserializing JSON into an array that arrived empty, or test data with a single fixture.
Related errors
- k must be between 1 and the size of the array
- Array must be non-empty.
- Array must be non-empty.
- Array must be non-empty.
- array must be non-empty.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/86abb755901260f3.
Report an issue: GitHub.