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 9 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
48 changes: 48 additions & 0 deletions src/main/java/com/thealgorithms/maths/SecondMinMax.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 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 )
*/

private SecondMinMax() {
}

public static int findSecondMin(final int[] arr) {
int secMin = Integer.MAX_VALUE, min = Integer.MAX_VALUE;
checkInput(arr);
for (final 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 Maximum / Minimum in array full of same numbers");
return secMin;
}

public static int findSecondMax(final int[] arr) {
int secMax = Integer.MIN_VALUE, max = Integer.MIN_VALUE;
checkInput(arr);
for (final int num : arr)
if (num > max) {
secMax = max;
max = num;
} else if ((num > secMax) && (num != max))
secMax = num;
if (secMax == Integer.MIN_VALUE) throw new IllegalArgumentException("Cant find Second Maximum / Minimum in array full of same numbers");
return secMax;
}

private static void checkInput(final int[] arr) {
if (arr.length == 0)
throw new IllegalArgumentException("Cant find Second Maximum / Minimum for empty array");
else if (arr.length == 1)
throw new IllegalArgumentException("Cant find Second Maximum / Minimum for single element");
}
}
39 changes: 39 additions & 0 deletions src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.thealgorithms.maths;

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

import org.junit.jupiter.api.Test;

public class SecondMinMaxTest {

private static final String EXP_MSG_ARR_IS_EMPTY = "Cant find Second Maximum / Minimum for empty array";
private static final String EXP_MSG_ARR_SINGLE_ELE = "Cant find Second Maximum / Minimum for single element";
private static final String EXP_MSG_ARR_SAME_ELE = "Cant find Second Maximum / Minimum in array full of same numbers";

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

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

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

@Test
public void testForValidOutput() {
assertEquals(2, SecondMinMax.findSecondMin(new int[] {1, 2}));
assertEquals(1, SecondMinMax.findSecondMax(new int[] {1, 2}));
assertEquals(2, SecondMinMax.findSecondMin(new int[] {10, 1, 2, 4, 6, 3, 5, 7, 3, 2, 5, 7, 4, 2, 1, 212, 343, 565, 2232, 65565}));
assertEquals(2232, SecondMinMax.findSecondMax(new int[] {10, 1, 2, 4, 6, 3, 5, 7, 3, 2, 5, 7, 4, 2, 1, 212, 343, 565, 2232, 65565}));
}
}