ronantakizawa/codereview-bench huggingface dataset:
Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/maths/FindMaxTest.java (Java)

Code before:
package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.stream.Stream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class FindMaxTest {

    @ParameterizedTest
    @MethodSource("inputStream")
    void numberTests(int expected, int[] input) {
        Assertions.assertEquals(expected, FindMax.findMax(input));
    }

    private static Stream<Arguments> inputStream() {
        return Stream.of(Arguments.of(10, new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}), Arguments.of(5, new int[] {5, 5, 5, 5, 5}), Arguments.of(0, new int[] {-1, 0}), Arguments.of(-1, new int[] {-10, -9, -8, -7, -6, -5, -4, -3, -2, -1}));
    }

    @Test
    public void testFindMaxThrowsExceptionForEmptyInput() {
        assertThrows(IllegalArgumentException.class, () -> FindMin.findMin(new int[] {}));
    }
}


Review comment (suggestion):
I would highly recommend to add test cases with arrays that are not sorted.

```suggestion
        return Stream.of(Arguments.of(10, new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}), Arguments.of(5, new int[] {5, 5, 5, 5, 5}), Arguments.of(0, new int[] {-1, 0}), Arguments.of(-1, new int[] {-10, -9, -8, -7, -6, -5, -4, -3, -2, -1}), Arguments.of(9, new int[] {3, -2, 3, 9, -4, -4, 8}));
```

Code after:
package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.stream.Stream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class FindMaxTest {

    @ParameterizedTest
    @MethodSource("inputStream")
    void numberTests(int expected, int[] input) {
        Assertions.assertEquals(expected, FindMax.findMax(input));
    }

    private static Stream<Arguments> inputStream() {
        return Stream.of(Arguments.of(10, new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}), Arguments.of(5, new int[] {5, 5, 5, 5, 5}), Arguments.of(0, new int[] {-1, 0}), Arguments.of(-1, new int[] {-10, -9, -8, -7, -6, -5, -4, -3, -2, -1}), Arguments.of(9, new int[] {3, -2, 3, 9, -4, -4, 8}));
    }

    @Test
    public void testFindMaxThrowsExceptionForEmptyInput() {
        assertThrows(IllegalArgumentException.class, () -> FindMax.findMax(new int[] {}));
    }
}
Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/maths/FindMaxTest.java (Java)

Code before:
package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.stream.Stream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class FindMaxTest {

    @ParameterizedTest
    @MethodSource("inputStream")
    void numberTests(int expected, int[] input) {
        Assertions.assertEquals(expected, FindMax.findMax(input));
    }

    private static Stream<Arguments> inputStream() {
        return Stream.of(Arguments.of(10, new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}), Arguments.of(5, new int[] {5, 5, 5, 5, 5}), Arguments.of(0, new int[] {-1, 0}), Arguments.of(-1, new int[] {-10, -9, -8, -7, -6, -5, -4, -3, -2, -1}));
    }

    @Test
    public void testFindMaxThrowsExceptionForEmptyInput() {
        assertThrows(IllegalArgumentException.class, () -> FindMin.findMin(new int[] {}));
    }
}


Review comment (suggestion):
```suggestion
        assertThrows(IllegalArgumentException.class, () -> FindMax.findMax(new int[] {}));
```

Code after:
package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.stream.Stream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class FindMaxTest {

    @ParameterizedTest
    @MethodSource("inputStream")
    void numberTests(int expected, int[] input) {
        Assertions.assertEquals(expected, FindMax.findMax(input));
    }

    private static Stream<Arguments> inputStream() {
        return Stream.of(Arguments.of(10, new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}), Arguments.of(5, new int[] {5, 5, 5, 5, 5}), Arguments.of(0, new int[] {-1, 0}), Arguments.of(-1, new int[] {-10, -9, -8, -7, -6, -5, -4, -3, -2, -1}), Arguments.of(9, new int[] {3, -2, 3, 9, -4, -4, 8}));
    }

    @Test
    public void testFindMaxThrowsExceptionForEmptyInput() {
        assertThrows(IllegalArgumentException.class, () -> FindMax.findMax(new int[] {}));
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/maths/FindMaxRecursion.java (Java)

Code before:
     * @param array contains elements
     * @param low the index of the first element
     * @param high the index of the last element
     * @return max of {@code array}
     */
    public static int max(int[] array, int low, int high) {
        if (low == high) {
            return array[low]; // or array[high]
        }

        int mid = (low + high) >>> 1;

        int leftMax = max(array, low, mid); // get max in [low, mid]
        int rightMax = max(array, mid + 1, high); // get max in [mid+1, high]

        return leftMax < rightMax ? rightMax : leftMax;
    }

    /**
     * Get max of array using recursion algorithm
     *
     * @param array contains elements
     * @return max value of {@code array}
     */
    public static int max(int[] array) {
        return array.length == 1 ? array[0] : max(array, 0, array.length - 1);
    }
}


Review comment (suggestion):
```suggestion
        return max(array, 0, array.length - 1);
```

Code after:
     * Get max of array using divide and conquer algorithm
     *
     * @param array contains elements
     * @param low the index of the first element
     * @param high the index of the last element
     * @return max of {@code array}
     */
    public static int max(final int[] array, final int low, final int high) {
        if (array.length == 0) {
            throw new IllegalArgumentException("array must be non-empty.");
        }
        if (low == high) {
            return array[low]; // or array[high]
        }

        int mid = (low + high) >>> 1;

        int leftMax = max(array, low, mid); // get max in [low, mid]
        int rightMax = max(array, mid + 1, high); // get max in [mid+1, high]

        return leftMax < rightMax ? rightMax : leftMax;
    }

    /**
     * Get max of array using recursion algorithm
     *
     * @param array contains elements
     * @return max value of {@code array}
     */
    public static int max(final int[] array) {
        return max(array, 0, array.length - 1);
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/maths/FindMaxRecursion.java (Java)

Code before:
     *
     * @param array contains elements
     * @param low the index of the first element
     * @param high the index of the last element
     * @return max of {@code array}
     */
    public static int max(int[] array, int low, int high) {
        if (low == high) {
            return array[low]; // or array[high]
        }

        int mid = (low + high) >>> 1;

        int leftMax = max(array, low, mid); // get max in [low, mid]
        int rightMax = max(array, mid + 1, high); // get max in [mid+1, high]

        return leftMax < rightMax ? rightMax : leftMax;
    }

    /**
     * Get max of array using recursion algorithm
     *
     * @param array contains elements
     * @return max value of {@code array}
     */
    public static int max(int[] array) {
        return array.length == 1 ? array[0] : max(array, 0, array.length - 1);
    }
}


Review comment (suggestion):
```suggestion
    public static int max(final int[] array) {
```

Please also mark the arguments of the _other_ `max` method as `final`.

Code after:
    /**
     * Get max of array using divide and conquer algorithm
     *
     * @param array contains elements
     * @param low the index of the first element
     * @param high the index of the last element
     * @return max of {@code array}
     */
    public static int max(final int[] array, final int low, final int high) {
        if (array.length == 0) {
            throw new IllegalArgumentException("array must be non-empty.");
        }
        if (low == high) {
            return array[low]; // or array[high]
        }

        int mid = (low + high) >>> 1;

        int leftMax = max(array, low, mid); // get max in [low, mid]
        int rightMax = max(array, mid + 1, high); // get max in [mid+1, high]

        return leftMax < rightMax ? rightMax : leftMax;
    }

    /**
     * Get max of array using recursion algorithm
     *
     * @param array contains elements
     * @return max value of {@code array}
     */
    public static int max(final int[] array) {
        return max(array, 0, array.length - 1);
    }
}
Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/maths/FindMaxRecursionTest.java (Java)

Code before:
package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.stream.Stream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class FindMaxRecursionTest {

    @ParameterizedTest
    @MethodSource("inputStream")
    void numberTests(int expected, int[] input) {
        Assertions.assertEquals(expected, FindMaxRecursion.max(input));
    }

    private static Stream<Arguments> inputStream() {
        return Stream.of(Arguments.of(10, new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}), Arguments.of(5, new int[] {5, 5, 5, 5, 5}), Arguments.of(0, new int[] {-1, 0}), Arguments.of(-1, new int[] {-10, -9, -8, -7, -6, -5, -4, -3, -2, -1}), Arguments.of(9, new int[] {3, -2, 3, 9, -4, -4, 8}));
    }

    @Test
    public void testFindMaxThrowsExceptionForEmptyInput() {
        assertThrows(IllegalArgumentException.class, () -> FindMax.findMax(new int[] {}));
    }
}


Review comment (suggestion):
```suggestion
        assertThrows(IllegalArgumentException.class, () -> FindMaxRecursion.max(new int[] {}));
```

Code after:
package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.stream.Stream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class FindMaxRecursionTest {

    @ParameterizedTest
    @MethodSource("inputStream")
    void numberTests(int expected, int[] input) {
        Assertions.assertEquals(expected, FindMaxRecursion.max(input));
    }

    private static Stream<Arguments> inputStream() {
        return Stream.of(Arguments.of(5, new int[] {5, 5, 5, 5, 5}), Arguments.of(0, new int[] {-1, 0}), Arguments.of(-1, new int[] {-10, -9, -8, -7, -6, -5, -4, -3, -2, -1}), Arguments.of(9, new int[] {3, -2, 3, 9, -4, -4, 8}), Arguments.of(3, new int[] {3}));
    }

    @Test
    public void testFindMaxThrowsExceptionForEmptyInput() {
        assertThrows(IllegalArgumentException.class, () -> FindMaxRecursion.max(new int[] {}));
    }
}
Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/maths/FindMaxRecursionTest.java (Java)

Code before:
package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.stream.Stream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class FindMaxRecursionTest {

    @ParameterizedTest
    @MethodSource("inputStream")
    void numberTests(int expected, int[] input) {
        Assertions.assertEquals(expected, FindMaxRecursion.max(input));
    }

    private static Stream<Arguments> inputStream() {
        return Stream.of(Arguments.of(10, new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}), Arguments.of(5, new int[] {5, 5, 5, 5, 5}), Arguments.of(0, new int[] {-1, 0}), Arguments.of(-1, new int[] {-10, -9, -8, -7, -6, -5, -4, -3, -2, -1}), Arguments.of(9, new int[] {3, -2, 3, 9, -4, -4, 8}));
    }

    @Test
    public void testFindMaxThrowsExceptionForEmptyInput() {
        assertThrows(IllegalArgumentException.class, () -> FindMax.findMax(new int[] {}));
    }
}


Review comment (suggestion):
```suggestion
        return Stream.of(Arguments.of(10, new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}), Arguments.of(5, new int[] {5, 5, 5, 5, 5}), Arguments.of(0, new int[] {-1, 0}), Arguments.of(-1, new int[] {-10, -9, -8, -7, -6, -5, -4, -3, -2, -1}), Arguments.of(9, new int[] {3, -2, 3, 9, -4, -4, 8}), Arguments.of(3, new int[] {3}));
```

Feel free to remove one of the test cases with _sorted_ array - there are four of them.

Code after:
package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.stream.Stream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class FindMaxRecursionTest {

    @ParameterizedTest
    @MethodSource("inputStream")
    void numberTests(int expected, int[] input) {
        Assertions.assertEquals(expected, FindMaxRecursion.max(input));
    }

    private static Stream<Arguments> inputStream() {
        return Stream.of(Arguments.of(5, new int[] {5, 5, 5, 5, 5}), Arguments.of(0, new int[] {-1, 0}), Arguments.of(-1, new int[] {-10, -9, -8, -7, -6, -5, -4, -3, -2, -1}), Arguments.of(9, new int[] {3, -2, 3, 9, -4, -4, 8}), Arguments.of(3, new int[] {3}));
    }

    @Test
    public void testFindMaxThrowsExceptionForEmptyInput() {
        assertThrows(IllegalArgumentException.class, () -> FindMaxRecursion.max(new int[] {}));
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/maths/FindMaxRecursion.java (Java)

Code before:
package com.thealgorithms.maths;

public final class FindMaxRecursion {

    private FindMaxRecursion() {     
    }
    /**
     * Get max of array using divide and conquer algorithm
     *
     * @param array contains elements
     * @param low the index of the first element
     * @param high the index of the last element
     * @return max of {@code array}
     */
    public static int max(final int[] array, final int low, final int high) {
        if (array.length == 0) {
            throw new IllegalArgumentException("array must be non-empty.");
        }
        if (low == high) {
            return array[low]; // or array[high]
        }

        int mid = (low + high) >>> 1;

        int leftMax = max(array, low, mid); // get max in [low, mid]
        int rightMax = max(array, mid + 1, high); // get max in [mid+1, high]

        return leftMax < rightMax ? rightMax : leftMax;
    }


Review comment (suggestion):
```suggestion
    private FindMaxRecursion() {
```

Code after:
package com.thealgorithms.maths;

public final class FindMaxRecursion {

    private FindMaxRecursion() {
    }
    /**
     * Get max of array using divide and conquer algorithm
     *
     * @param array contains elements
     * @param low the index of the first element
     * @param high the index of the last element
     * @return max of {@code array}
     */
    public static int max(final int[] array, final int low, final int high) {
        if (array.length == 0) {
            throw new IllegalArgumentException("array must be non-empty.");
        }
        if (low == high) {
            return array[low]; // or array[high]
        }

        int mid = (low + high) >>> 1;

        int leftMax = max(array, low, mid); // get max in [low, mid]
        int rightMax = max(array, mid + 1, high); // get max in [mid+1, high]

        return leftMax < rightMax ? rightMax : leftMax;
    }
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/maths/SecondMinMax.java (Java)

Code before:
package com.thealgorithms.maths;

public class SecondMinMax {

    /**
     * @brief Finds the Second minimum / maximum value from the array
     * @param int array
     * @exception IllegalArgumentException if input array is empty or length 1 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(int[] arr) {
        int secMin = Integer.MAX_VALUE, min = Integer.MAX_VALUE;
        if (arr.length == 0)
            throw new IllegalArgumentException("Cant find Second Minimum for empty array");
        else if (arr.length == 1)
            throw new IllegalArgumentException("Cant find Second Minimum for single element");
        else
            for (int num : arr)
                if (num < min) {
                    secMin = min;
                    min = num;
                } else if ((num < secMin) && (num != min))
                    secMin = num;
        if (secMin == Integer.MAX_VALUE) throw new IllegalArgumentException("Cant find Second Minimum in array full of same numbers");
        return secMin;
    }

Review comment (refactor):
You can make a _proper utility class_ of this.

```suggestion
public final class SecondMinMax {
    private SecondMinMax() {
    }
```

Code after:
package com.thealgorithms.maths;

import java.util.function.BiPredicate;

public final class SecondMinMax {

    /**
     * Utility class for finding second maximum or minimum based on BiPredicate
     * @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() {
    }

    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;
            }
        }
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/maths/SecondMinMax.java (Java)

Code before:
            } else if ((isBetter.test(num, secBest)) && (num != best)) {
                secBest = num;
            }
        }
        checkOutput(secBest, initialVal);
        return secBest;
    }

    /**
     * @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 int checkOutput(final int secNum, int initialVal) {
        if (secNum == initialVal) throw new IllegalArgumentException("Input array should have at least 2 distinct elements");
        return secNum;
    }
}


Review comment (suggestion):
```suggestion
        if (arr.length < 2) {
            throw new IllegalArgumentException("Input array must have length of at least two");
        }
```

Code after:
                best = num;
            } else if ((isBetter.test(num, secBest)) && (num != best)) {
                secBest = num;
            }
        }
        checkOutput(secBest, initialVal);
        return secBest;
    }

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