-
Notifications
You must be signed in to change notification settings - Fork 20k
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
Add SecondMinMax
#4432
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 046b6d7
Clang-format-lint error resolved
BharathSanjeeviT 679f6a2
Clang-format-error 2
BharathSanjeeviT 45e6e07
Added Program to find Second Minimum/Maximum element
BharathSanjeeviT 3c9e548
Test File & few changes
BharathSanjeeviT 718ca30
Clang-lint-error resolved
BharathSanjeeviT 00bd64f
Maven Build Error Resolved
BharathSanjeeviT ede4cbf
Clang-lint-error resolved
BharathSanjeeviT cff823d
Clang-lint-error resolved 2
BharathSanjeeviT b96924a
Changes Resolved
BharathSanjeeviT eb7dd97
Test Arguements are Streamed
BharathSanjeeviT a785b7a
Clang-lint-error resolved
BharathSanjeeviT b1efe06
Merge remote-tracking branch 'upstream/master' into secondMinMax
BharathSanjeeviT ebe3c02
incresed code reusability
BharathSanjeeviT 8e92077
Added Program to find Second Min / Max
BharathSanjeeviT ed63297
Program to find Second Min / Max
BharathSanjeeviT 9a52a12
Program to find Second Minimum / Maximum
BharathSanjeeviT 07d23aa
Merge remote-tracking branch 'upstream/master' into secondMinMax
BharathSanjeeviT c577f98
Program to find Second Best Number
BharathSanjeeviT 592bf7b
style: mark `initialVal` as `final`
vil02 56aeb71
style: resolve `MultipleVariableDeclarations`
vil02 088c13b
Merge branch 'master' into secondMinMax
vil02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
BharathSanjeeviT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (secMin == Integer.MAX_VALUE) throw new IllegalArgumentException("Cant find Second Maximum / Minimum in array full of same numbers"); | ||
BharathSanjeeviT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return secMin; | ||
} | ||
|
||
public static int findSecondMax(final int[] arr) { | ||
int secMax = Integer.MIN_VALUE, max = Integer.MIN_VALUE; | ||
checkInput(arr); | ||
BharathSanjeeviT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (final int num : arr) | ||
if (num > max) { | ||
secMax = max; | ||
max = num; | ||
} else if ((num > secMax) && (num != max)) | ||
secMax = num; | ||
BharathSanjeeviT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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"); | ||
BharathSanjeeviT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})); | ||
} | ||
BharathSanjeeviT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.