{"record":{"id":"86abb755901260f3","repo":"TheAlgorithms/Java","slug":"input-array-must-have-length-of-at-least-two","errorCode":null,"errorMessage":"Input array must have length of at least two","messagePattern":"Input array must have length of at least two","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/SecondMinMax.java","lineNumber":51,"sourceCode":"    /**\n     * @brief Finds the Second minimum / maximum value from the array\n     * @param arr the input array\n     * @exception IllegalArgumentException => if input array is of length less than 2 also if all elements are same\n     * @return the second minimum / maximum value from the input array\n     * @author Bharath Sanjeevi ( https://github.com/BharathSanjeeviT )\n     */\n\n    public static int findSecondMin(final int[] arr) {\n        return secondBest(arr, Integer.MAX_VALUE, (a, b) -> a < b);\n    }\n\n    public static int findSecondMax(final int[] arr) {\n        return secondBest(arr, Integer.MIN_VALUE, (a, b) -> a > b);\n    }\n\n    private static void checkInput(final int[] arr) {\n        if (arr.length < 2) {\n            throw new IllegalArgumentException(\"Input array must have length of at least two\");\n        }\n    }\n\n    private static void checkOutput(final int secNum, final int initialVal) {\n        if (secNum == initialVal) {\n            throw new IllegalArgumentException(\"Input array should have at least 2 distinct elements\");\n        }\n    }\n}\n","sourceCodeStart":33,"sourceCodeEnd":61,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/SecondMinMax.java#L33-L61","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nint second = SecondMinMax.findSecondMin(arr);\n\n// after\nif (arr == null || arr.length < 2) {\n    throw new IllegalArgumentException(\"Need at least 2 elements, got \" + (arr == null ? 0 : arr.length));\n}\nint second = SecondMinMax.findSecondMin(arr);","handlingStrategy":"validation","validationCode":"if (arr == null || arr.length < 2) {\n    throw new IllegalArgumentException(\"Need >= 2 elements\");\n}\nint s = SecondMinMax.findSecondMin(arr);","typeGuard":null,"tryCatchPattern":"try {\n    int s = SecondMinMax.findSecondMin(arr);\n} catch (IllegalArgumentException e) {\n    return OptionalInt.empty();\n}","preventionTips":["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."],"tags":["math","array","input-validation","argument-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}