Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/maths/SecondMinMax.java (Java)

Code before:
package com.thealgorithms.maths;

public final class SecondMinMax {

    /**
     * @brief Finds the Second minimum / maximum value from the array
     * @param int 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 )
     */

    private SecondMinMax() {
    }

    public static int findSecondMin(final int[] arr) {
        checkInput(arr);
        int secMin = Integer.MAX_VALUE, min = Integer.MAX_VALUE;
        for (final int num : arr)
            if (num < min) {
                secMin = min;
                min = num;
            } else if ((num < secMin) && (num != min)) {
                secMin = num;
            }
        return checkOutput(secMin);
    }

    public static int findSecondMax(final int[] arr) {
        checkInput(arr);
        int secMax = Integer.MIN_VALUE, max = Integer.MIN_VALUE;
        for (final int num : arr)
            if (num > max) {
                secMax = max;
                max = num;
            } else if ((num > secMax) && (num != max)) {
                secMax = num;
            }
        return checkOutput(secMax);
    }

    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 int checkOutput(final int secNum) {
        if ((secNum == Integer.MAX_VALUE) || (secNum == Integer.MIN_VALUE)) throw new IllegalArgumentException("Input array should have at least 2 distinct elements");
        return secNum;
    }
}


Review comment (security):
As suggested in [here](https://github.com/TheAlgorithms/Java/pull/4432#pullrequestreview-1651654653):

```suggestion
package com.thealgorithms.maths;

import java.util.function.BiPredicate;

public final class SecondMinMax {

    /**
     * @brief Utility class finding second smallest or gatest eleent in the input array
     * @author Bharath Sanjeevi ( https://github.com/BharathSanjeeviT )
     */

    private SecondMinMax() {
    }

    private static int secondBest(final int[] arr, final int initialVal, final BiPredicate<Integer, Integer> isBetter) {
        checkInput(arr);
        int best = initialVal;
        int secBest = initialVal;
        for (final int num : arr) {
            if (isBetter.test(num, best)) {
                secBest = best;
                best = num;
            } else if ((isBetter.test(num, secBest)) && (num != best)) {
                secBest = num;
            }
        }
        checkOutput(initialVal, secBest);
        return secBest;
    }

    /**
     * @brief Finds the Second smallest 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 smallest value from the input array
     */
    public static int findSecondMin(final int[] arr) {
        return secondBest(arr, Integer.MAX_VALUE, (a, b) -> a < b);
    }

    /**
     * @brief Finds the Second largest 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 largest value from the input array
     */
    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 initialVal, final int secBest) {
        if (secBest == initialVal) {
            throw new IllegalArgumentException("Input array should have at least 2 distinct elements");
        }
    }
}
```

Code after:

    /**
     * @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");
        }
    }
}