Skip to content

Add SecondMinMax #4432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
06186c2
Added Second Min/Max program
BharathSanjeeviT Sep 29, 2023
046b6d7
Clang-format-lint error resolved
BharathSanjeeviT Sep 29, 2023
679f6a2
Clang-format-error 2
BharathSanjeeviT Sep 29, 2023
45e6e07
Added Program to find Second Minimum/Maximum element
BharathSanjeeviT Sep 29, 2023
3c9e548
Test File & few changes
BharathSanjeeviT Sep 30, 2023
718ca30
Clang-lint-error resolved
BharathSanjeeviT Sep 30, 2023
00bd64f
Maven Build Error Resolved
BharathSanjeeviT Sep 30, 2023
ede4cbf
Clang-lint-error resolved
BharathSanjeeviT Sep 30, 2023
cff823d
Clang-lint-error resolved 2
BharathSanjeeviT Sep 30, 2023
b96924a
Changes Resolved
BharathSanjeeviT Sep 30, 2023
eb7dd97
Test Arguements are Streamed
BharathSanjeeviT Sep 30, 2023
a785b7a
Clang-lint-error resolved
BharathSanjeeviT Sep 30, 2023
b1efe06
Merge remote-tracking branch 'upstream/master' into secondMinMax
BharathSanjeeviT Sep 30, 2023
ebe3c02
incresed code reusability
BharathSanjeeviT Sep 30, 2023
8e92077
Added Program to find Second Min / Max
BharathSanjeeviT Sep 30, 2023
ed63297
Program to find Second Min / Max
BharathSanjeeviT Oct 1, 2023
9a52a12
Program to find Second Minimum / Maximum
BharathSanjeeviT Oct 1, 2023
07d23aa
Merge remote-tracking branch 'upstream/master' into secondMinMax
BharathSanjeeviT Oct 1, 2023
c577f98
Program to find Second Best Number
BharathSanjeeviT Oct 1, 2023
592bf7b
style: mark `initialVal` as `final`
vil02 Oct 1, 2023
56aeb71
style: resolve `MultipleVariableDeclarations`
vil02 Oct 1, 2023
088c13b
Merge branch 'master' into secondMinMax
vil02 Oct 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/main/java/com/thealgorithms/maths/SecondMinMax.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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;
}
}
55 changes: 55 additions & 0 deletions src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.assertEquals;
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 SecondMinMaxTest {

private static final String EXP_MSG_ARR_LEN_LESS_2 = "Input array must have length of at least two";
private static final String EXP_MSG_ARR_SAME_ELE = "Input array should have at least 2 distinct elements";

@Test
public void testForEmptyInputArray() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> SecondMinMax.findSecondMin(new int[] {}));
assertEquals(exception.getMessage(), EXP_MSG_ARR_LEN_LESS_2);
}

@Test
public void testForArrayWithSingleElement() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> SecondMinMax.findSecondMax(new int[] {1}));
assertEquals(exception.getMessage(), EXP_MSG_ARR_LEN_LESS_2);
}

@Test
public void testForArrayWithSameElements() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> SecondMinMax.findSecondMin(new int[] {1, 1, 1, 1}));
assertEquals(exception.getMessage(), EXP_MSG_ARR_SAME_ELE);
}

@ParameterizedTest
@MethodSource("inputStreamMin")
void numberTestsMin(int expected, int[] input) {
Assertions.assertEquals(expected, SecondMinMax.findSecondMin(input));
}

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

@ParameterizedTest
@MethodSource("inputStreamMax")
void numberTestsMax(int expected, int[] input) {
Assertions.assertEquals(expected, SecondMinMax.findSecondMax(input));
}

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