Skip to content

Commit 37b3844

Browse files
Add SecondMinMax (#4432)
* Added Second Min/Max program * Clang-format-lint error resolved * Clang-format-error 2 * Added Program to find Second Minimum/Maximum element * Test File & few changes * Clang-lint-error resolved * Maven Build Error Resolved * Clang-lint-error resolved * Clang-lint-error resolved 2 * Changes Resolved * Test Arguements are Streamed * Clang-lint-error resolved * incresed code reusability * Added Program to find Second Min / Max * Program to find Second Min / Max * Program to find Second Minimum / Maximum * Program to find Second Best Number * style: mark `initialVal` as `final` * style: resolve `MultipleVariableDeclarations` Each variable declaration must be in its own statement. --------- Co-authored-by: Piotr Idzik <[email protected]>
1 parent da687c1 commit 37b3844

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.thealgorithms.maths;
2+
3+
import java.util.function.BiPredicate;
4+
5+
public final class SecondMinMax {
6+
7+
/**
8+
* Utility class for finding second maximum or minimum based on BiPredicate
9+
* @exception IllegalArgumentException => if input array is of length less than 2 also if all elements are same
10+
* @return the second minimum / maximum value from the input array
11+
* @author Bharath Sanjeevi ( https://github.com/BharathSanjeeviT )
12+
*/
13+
14+
private SecondMinMax() {
15+
}
16+
17+
private static int secondBest(final int[] arr, final int initialVal, final BiPredicate<Integer, Integer> isBetter) {
18+
checkInput(arr);
19+
int best = initialVal;
20+
int secBest = initialVal;
21+
for (final int num : arr) {
22+
if (isBetter.test(num, best)) {
23+
secBest = best;
24+
best = num;
25+
} else if ((isBetter.test(num, secBest)) && (num != best)) {
26+
secBest = num;
27+
}
28+
}
29+
checkOutput(secBest, initialVal);
30+
return secBest;
31+
}
32+
33+
/**
34+
* @brief Finds the Second minimum / maximum value from the array
35+
* @param arr the input array
36+
* @exception IllegalArgumentException => if input array is of length less than 2 also if all elements are same
37+
* @return the second minimum / maximum value from the input array
38+
* @author Bharath Sanjeevi ( https://github.com/BharathSanjeeviT )
39+
*/
40+
41+
public static int findSecondMin(final int[] arr) {
42+
return secondBest(arr, Integer.MAX_VALUE, (a, b) -> a < b);
43+
}
44+
45+
public static int findSecondMax(final int[] arr) {
46+
return secondBest(arr, Integer.MIN_VALUE, (a, b) -> a > b);
47+
}
48+
49+
private static void checkInput(final int[] arr) {
50+
if (arr.length < 2) {
51+
throw new IllegalArgumentException("Input array must have length of at least two");
52+
}
53+
}
54+
55+
private static void checkOutput(final int secNum, final int initialVal) {
56+
if (secNum == initialVal) {
57+
throw new IllegalArgumentException("Input array should have at least 2 distinct elements");
58+
}
59+
}
60+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.api.Assertions;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.params.ParameterizedTest;
10+
import org.junit.jupiter.params.provider.Arguments;
11+
import org.junit.jupiter.params.provider.MethodSource;
12+
13+
public class SecondMinMaxTest {
14+
15+
private static final String EXP_MSG_ARR_LEN_LESS_2 = "Input array must have length of at least two";
16+
private static final String EXP_MSG_ARR_SAME_ELE = "Input array should have at least 2 distinct elements";
17+
18+
public static class TestCase {
19+
public TestCase(final int[] inInputArray, final int inSecondMin, final int inSecondMax) {
20+
inputArray = inInputArray;
21+
secondMin = inSecondMin;
22+
secondMax = inSecondMax;
23+
}
24+
final int[] inputArray;
25+
final int secondMin;
26+
final int secondMax;
27+
}
28+
29+
@Test
30+
public void testForEmptyInputArray() {
31+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> SecondMinMax.findSecondMin(new int[] {}));
32+
assertEquals(exception.getMessage(), EXP_MSG_ARR_LEN_LESS_2);
33+
}
34+
35+
@Test
36+
public void testForArrayWithSingleElement() {
37+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> SecondMinMax.findSecondMax(new int[] {1}));
38+
assertEquals(exception.getMessage(), EXP_MSG_ARR_LEN_LESS_2);
39+
}
40+
41+
@Test
42+
public void testForArrayWithSameElements() {
43+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> SecondMinMax.findSecondMin(new int[] {1, 1, 1, 1}));
44+
assertEquals(exception.getMessage(), EXP_MSG_ARR_SAME_ELE);
45+
}
46+
47+
@ParameterizedTest
48+
@MethodSource("inputStream")
49+
void numberTests(final TestCase tc) {
50+
Assertions.assertEquals(tc.secondMax, SecondMinMax.findSecondMax(tc.inputArray));
51+
Assertions.assertEquals(tc.secondMin, SecondMinMax.findSecondMin(tc.inputArray));
52+
}
53+
54+
private static Stream<Arguments> inputStream() {
55+
return Stream.of(Arguments.of(new TestCase(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 2, 9)), Arguments.of(new TestCase(new int[] {5, 4, 5, 5, 5}, 5, 4)), Arguments.of(new TestCase(new int[] {-1, 0}, 0, -1)),
56+
Arguments.of(new TestCase(new int[] {-10, -9, -8, -7, -6, -5, -4, -3, -2, -1}, -9, -2)), Arguments.of(new TestCase(new int[] {3, -2, 3, 9, -4, -4, 8}, -2, 8)));
57+
}
58+
}

0 commit comments

Comments
 (0)