From 06186c25b788ffac10a7d52c2a09124f3ebaa332 Mon Sep 17 00:00:00 2001 From: BharathSanjeeviT Date: Fri, 29 Sep 2023 12:53:26 +0530 Subject: [PATCH 01/19] Added Second Min/Max program --- .../com/thealgorithms/maths/SecondMinMax.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/java/com/thealgorithms/maths/SecondMinMax.java diff --git a/src/main/java/com/thealgorithms/maths/SecondMinMax.java b/src/main/java/com/thealgorithms/maths/SecondMinMax.java new file mode 100644 index 000000000000..175f8bf4cc17 --- /dev/null +++ b/src/main/java/com/thealgorithms/maths/SecondMinMax.java @@ -0,0 +1,49 @@ +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 ; + } + + public static int findSecondMax( int[] arr ) { + int secMax = Integer.MIN_VALUE , max = Integer.MIN_VALUE ; + if ( arr.length == 0 ) + throw new IllegalArgumentException("Cant find Second Maximum for empty array"); + else if ( arr.length == 1 ) + throw new IllegalArgumentException("Cant find Second Maximum for single element"); + else + for ( 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 in array full of same numbers") ; + return secMax ; + } + +} From 046b6d76917d0a93d93707027fd05a702a695aa4 Mon Sep 17 00:00:00 2001 From: BharathSanjeeviT Date: Fri, 29 Sep 2023 13:32:13 +0530 Subject: [PATCH 02/19] Clang-format-lint error resolved --- .../com/thealgorithms/maths/SecondMinMax.java | 53 +++++++++---------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/SecondMinMax.java b/src/main/java/com/thealgorithms/maths/SecondMinMax.java index 175f8bf4cc17..12e4ab13d2e0 100644 --- a/src/main/java/com/thealgorithms/maths/SecondMinMax.java +++ b/src/main/java/com/thealgorithms/maths/SecondMinMax.java @@ -2,7 +2,7 @@ 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 @@ -10,40 +10,39 @@ public class SecondMinMax { * @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 ) + 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 ) + 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 ; + 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; } - public static int findSecondMax( int[] arr ) { - int secMax = Integer.MIN_VALUE , max = Integer.MIN_VALUE ; - if ( arr.length == 0 ) + public static int findSecondMax(int[] arr) { + int secMax = Integer.MIN_VALUE, max = Integer.MIN_VALUE; + if (arr.length == 0) throw new IllegalArgumentException("Cant find Second Maximum for empty array"); - else if ( arr.length == 1 ) + else if (arr.length == 1) throw new IllegalArgumentException("Cant find Second Maximum for single element"); else - for ( 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 in array full of same numbers") ; - return secMax ; + for (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 in array full of same numbers"); + return secMax; } } From 679f6a2e5d967768b7f9849c864c23649859b6c6 Mon Sep 17 00:00:00 2001 From: BharathSanjeeviT Date: Fri, 29 Sep 2023 13:36:37 +0530 Subject: [PATCH 03/19] Clang-format-error 2 --- src/main/java/com/thealgorithms/maths/SecondMinMax.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/SecondMinMax.java b/src/main/java/com/thealgorithms/maths/SecondMinMax.java index 12e4ab13d2e0..e1c5e0f51e4c 100644 --- a/src/main/java/com/thealgorithms/maths/SecondMinMax.java +++ b/src/main/java/com/thealgorithms/maths/SecondMinMax.java @@ -11,7 +11,7 @@ public class SecondMinMax { */ public static int findSecondMin(int[] arr) { - int secMin = Integer.MAX_VALUE, min = Integer.MAX_VALUE; + 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) @@ -23,13 +23,12 @@ else if (arr.length == 1) 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"); + if (secMin == Integer.MAX_VALUE) throw new IllegalArgumentException("Cant find Second Minimum in array full of same numbers"); return secMin; } public static int findSecondMax(int[] arr) { - int secMax = Integer.MIN_VALUE, max = Integer.MIN_VALUE; + int secMax = Integer.MIN_VALUE, max = Integer.MIN_VALUE; if (arr.length == 0) throw new IllegalArgumentException("Cant find Second Maximum for empty array"); else if (arr.length == 1) From 45e6e071153ba2a97e0f5dd95d3579f1821968af Mon Sep 17 00:00:00 2001 From: BharathSanjeeviT Date: Fri, 29 Sep 2023 13:40:04 +0530 Subject: [PATCH 04/19] Added Program to find Second Minimum/Maximum element --- src/main/java/com/thealgorithms/maths/SecondMinMax.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/maths/SecondMinMax.java b/src/main/java/com/thealgorithms/maths/SecondMinMax.java index e1c5e0f51e4c..55fb21d1f214 100644 --- a/src/main/java/com/thealgorithms/maths/SecondMinMax.java +++ b/src/main/java/com/thealgorithms/maths/SecondMinMax.java @@ -43,5 +43,4 @@ else if (arr.length == 1) if (secMax == Integer.MIN_VALUE) throw new IllegalArgumentException("Cant find Second Maximum in array full of same numbers"); return secMax; } - } From 3c9e5486ff9edec7bce45b7452935eef357fb7be Mon Sep 17 00:00:00 2001 From: BharathSanjeeviT Date: Sat, 30 Sep 2023 10:59:00 +0530 Subject: [PATCH 05/19] Test File & few changes --- .../thealgorithms/maths/SecondMinMaxTest.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java diff --git a/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java b/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java new file mode 100644 index 000000000000..1582a9f629d3 --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java @@ -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})); + } +} From 718ca30e8df8118b45b8b727352a467f2f238e3a Mon Sep 17 00:00:00 2001 From: BharathSanjeeviT Date: Sat, 30 Sep 2023 11:07:06 +0530 Subject: [PATCH 06/19] Clang-lint-error resolved --- .../thealgorithms/maths/SecondMinMaxTest.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java b/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java index 1582a9f629d3..2498cfa8ac11 100644 --- a/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java +++ b/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java @@ -12,28 +12,28 @@ public class SecondMinMaxTest { 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[]{})); + 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})); + 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})); + 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})); + 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})); } } From 00bd64fd06d8d5786ea92d7756f78d7f305a2584 Mon Sep 17 00:00:00 2001 From: BharathSanjeeviT Date: Sat, 30 Sep 2023 11:21:11 +0530 Subject: [PATCH 07/19] Maven Build Error Resolved --- .../com/thealgorithms/maths/SecondMinMax.java | 54 ++++++++++--------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/SecondMinMax.java b/src/main/java/com/thealgorithms/maths/SecondMinMax.java index 55fb21d1f214..931637f10649 100644 --- a/src/main/java/com/thealgorithms/maths/SecondMinMax.java +++ b/src/main/java/com/thealgorithms/maths/SecondMinMax.java @@ -1,6 +1,6 @@ package com.thealgorithms.maths; -public class SecondMinMax { +public final class SecondMinMax { /** * @brief Finds the Second minimum / maximum value from the array @@ -10,37 +10,39 @@ public class SecondMinMax { * @author Bharath Sanjeevi ( https://github.com/BharathSanjeeviT ) */ - public static int findSecondMin(int[] arr) { + private SecondMinMax() { + } + + public static int findSecondMin(final 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"); + 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(int[] arr) { + 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 for empty array"); + throw new IllegalArgumentException("Cant find Second Maximum / Minimum for empty array"); else if (arr.length == 1) - throw new IllegalArgumentException("Cant find Second Maximum for single element"); - else - for (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 in array full of same numbers"); - return secMax; + throw new IllegalArgumentException("Cant find Second Maximum / Minimum for single element"); } } From ede4cbf6121435e2135c9b77c06f7af834b4582f Mon Sep 17 00:00:00 2001 From: BharathSanjeeviT Date: Sat, 30 Sep 2023 11:25:49 +0530 Subject: [PATCH 08/19] Clang-lint-error resolved --- src/main/java/com/thealgorithms/maths/SecondMinMax.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/SecondMinMax.java b/src/main/java/com/thealgorithms/maths/SecondMinMax.java index 931637f10649..fd6b5aa3186c 100644 --- a/src/main/java/com/thealgorithms/maths/SecondMinMax.java +++ b/src/main/java/com/thealgorithms/maths/SecondMinMax.java @@ -15,7 +15,7 @@ private SecondMinMax() { public static int findSecondMin(final int[] arr) { int secMin = Integer.MAX_VALUE, min = Integer.MAX_VALUE; - checkInput( arr ) ; + checkInput( arr ); for (final int num : arr) if (num < min) { secMin = min; @@ -28,7 +28,7 @@ public static int findSecondMin(final int[] arr) { public static int findSecondMax(final int[] arr) { int secMax = Integer.MIN_VALUE, max = Integer.MIN_VALUE; - checkInput( arr ) ; + checkInput( arr ); for (final int num : arr) if (num > max) { secMax = max; From cff823d25023b4a8cf833fdd7ff85856fb9a8f56 Mon Sep 17 00:00:00 2001 From: BharathSanjeeviT Date: Sat, 30 Sep 2023 11:29:22 +0530 Subject: [PATCH 09/19] Clang-lint-error resolved 2 --- src/main/java/com/thealgorithms/maths/SecondMinMax.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/SecondMinMax.java b/src/main/java/com/thealgorithms/maths/SecondMinMax.java index fd6b5aa3186c..cd9007d0b58b 100644 --- a/src/main/java/com/thealgorithms/maths/SecondMinMax.java +++ b/src/main/java/com/thealgorithms/maths/SecondMinMax.java @@ -15,7 +15,7 @@ private SecondMinMax() { public static int findSecondMin(final int[] arr) { int secMin = Integer.MAX_VALUE, min = Integer.MAX_VALUE; - checkInput( arr ); + checkInput(arr); for (final int num : arr) if (num < min) { secMin = min; @@ -28,7 +28,7 @@ public static int findSecondMin(final int[] arr) { public static int findSecondMax(final int[] arr) { int secMax = Integer.MIN_VALUE, max = Integer.MIN_VALUE; - checkInput( arr ); + checkInput(arr); for (final int num : arr) if (num > max) { secMax = max; From b96924a920b5bae41fc7396cf9716ea3032df773 Mon Sep 17 00:00:00 2001 From: BharathSanjeeviT Date: Sat, 30 Sep 2023 13:22:38 +0530 Subject: [PATCH 10/19] Changes Resolved --- .../com/thealgorithms/maths/SecondMinMax.java | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/SecondMinMax.java b/src/main/java/com/thealgorithms/maths/SecondMinMax.java index cd9007d0b58b..5a91566e6f42 100644 --- a/src/main/java/com/thealgorithms/maths/SecondMinMax.java +++ b/src/main/java/com/thealgorithms/maths/SecondMinMax.java @@ -5,7 +5,7 @@ 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 + * @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 ) */ @@ -14,35 +14,38 @@ private SecondMinMax() { } public static int findSecondMin(final int[] arr) { - int secMin = Integer.MAX_VALUE, min = Integer.MAX_VALUE; 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)) + } 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; + } + return checkOutput(secMin); } public static int findSecondMax(final int[] arr) { - int secMax = Integer.MIN_VALUE, max = Integer.MIN_VALUE; 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)) + } 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; + } + return checkOutput(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"); + 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 ; } } From eb7dd974a3daf7b2218b62c69030c990ac799b3e Mon Sep 17 00:00:00 2001 From: BharathSanjeeviT Date: Sat, 30 Sep 2023 13:23:51 +0530 Subject: [PATCH 11/19] Test Arguements are Streamed --- .../thealgorithms/maths/SecondMinMaxTest.java | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java b/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java index 2498cfa8ac11..749d07bd97e9 100644 --- a/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java +++ b/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java @@ -1,26 +1,31 @@ package com.thealgorithms.maths; +import java.util.stream.Stream; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Assertions; +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_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"; + 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_IS_EMPTY); + 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_SINGLE_ELE); + assertEquals(exception.getMessage(), EXP_MSG_ARR_LEN_LESS_2); } @Test @@ -29,11 +34,23 @@ public void testForArrayWithSameElements() { 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})); + @ParameterizedTest + @MethodSource("inputStreamMin") + void numberTestsMin(int expected, int[] input) { + Assertions.assertEquals(expected, SecondMinMax.findSecondMin(input)); + } + + private static Stream 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 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})); } } From a785b7a4c98ca01c87a2c3423a3a9d7ed3a17aa0 Mon Sep 17 00:00:00 2001 From: BharathSanjeeviT Date: Sat, 30 Sep 2023 13:32:29 +0530 Subject: [PATCH 12/19] Clang-lint-error resolved --- src/main/java/com/thealgorithms/maths/SecondMinMax.java | 5 ++--- src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/SecondMinMax.java b/src/main/java/com/thealgorithms/maths/SecondMinMax.java index 5a91566e6f42..6b9c34bc4920 100644 --- a/src/main/java/com/thealgorithms/maths/SecondMinMax.java +++ b/src/main/java/com/thealgorithms/maths/SecondMinMax.java @@ -40,12 +40,11 @@ public static int findSecondMax(final int[] arr) { } private static void checkInput(final int[] arr) { - if (arr.length < 2) - throw new IllegalArgumentException("Input array must have length of at least two"); + 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 ; + return secNum; } } diff --git a/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java b/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java index 749d07bd97e9..c54aea13e2a2 100644 --- a/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java +++ b/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java @@ -1,12 +1,11 @@ package com.thealgorithms.maths; -import java.util.stream.Stream; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; -import org.junit.jupiter.api.Test; +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; From ebe3c0215290eb402e4dbdb183d800442de1e5ff Mon Sep 17 00:00:00 2001 From: BharathSanjeeviT Date: Sat, 30 Sep 2023 23:38:27 +0530 Subject: [PATCH 13/19] incresed code reusability --- .../com/thealgorithms/maths/SecondMinMax.java | 53 +++++++++++-------- .../thealgorithms/maths/SecondMinMaxTest.java | 32 +++++------ 2 files changed, 47 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/SecondMinMax.java b/src/main/java/com/thealgorithms/maths/SecondMinMax.java index 6b9c34bc4920..e6620852b832 100644 --- a/src/main/java/com/thealgorithms/maths/SecondMinMax.java +++ b/src/main/java/com/thealgorithms/maths/SecondMinMax.java @@ -1,10 +1,11 @@ package com.thealgorithms.maths; +import java.util.function.BiPredicate; + public final class SecondMinMax { /** - * @brief Finds the Second minimum / maximum value from the array - * @param int array + * 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 ) @@ -13,38 +14,44 @@ public final class SecondMinMax { private SecondMinMax() { } - public static int findSecondMin(final int[] arr) { + private static int secondBest(final int[] arr, final int initialVal, final BiPredicate isBetter) { 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; + int best = initialVal, 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; } - return checkOutput(secMin); + } + 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) { - 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); + 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) { - if ((secNum == Integer.MAX_VALUE) || (secNum == Integer.MIN_VALUE)) throw new IllegalArgumentException("Input array should have at least 2 distinct elements"); + 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; } } diff --git a/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java b/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java index c54aea13e2a2..a648b818df5d 100644 --- a/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java +++ b/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java @@ -15,6 +15,17 @@ 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"; + public static class TestCase { + public TestCase(final int[] inInputArray, final int inSecondMin, final int inSecondMax) { + inputArray = inInputArray; + secondMin = inSecondMin; + secondMax = inSecondMax; + } + final int[] inputArray; + final int secondMin; + final int secondMax; + } + @Test public void testForEmptyInputArray() { IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> SecondMinMax.findSecondMin(new int[] {})); @@ -34,22 +45,13 @@ public void testForArrayWithSameElements() { } @ParameterizedTest - @MethodSource("inputStreamMin") - void numberTestsMin(int expected, int[] input) { - Assertions.assertEquals(expected, SecondMinMax.findSecondMin(input)); - } - - private static Stream 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)); + @MethodSource("inputStream") + void numberTests(final TestCase tc) { + Assertions.assertEquals(tc.secondMax, SecondMinMax.findSecondMax(tc.inputArray)); + Assertions.assertEquals(tc.secondMin, SecondMinMax.findSecondMin(tc.inputArray)); } - private static Stream 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})); + private static Stream inputStream() { + 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, 5, 4, 5, 5}, 5, 4)), Arguments.of(new TestCase(new int[] {-1, 0}, 0, -1)), 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))); } } From 8e920774902bdb1702817aa4f4271b87511ddae5 Mon Sep 17 00:00:00 2001 From: BharathSanjeeviT Date: Sat, 30 Sep 2023 23:56:18 +0530 Subject: [PATCH 14/19] Added Program to find Second Min / Max --- src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java b/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java index a648b818df5d..c744614e5cfa 100644 --- a/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java +++ b/src/test/java/com/thealgorithms/maths/SecondMinMaxTest.java @@ -52,6 +52,7 @@ void numberTests(final TestCase tc) { } private static Stream inputStream() { - 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, 5, 4, 5, 5}, 5, 4)), Arguments.of(new TestCase(new int[] {-1, 0}, 0, -1)), 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))); + 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)), + 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))); } } From ed6329712c3f93fdc844389207d9db5d686b1ae6 Mon Sep 17 00:00:00 2001 From: BharathSanjeeviT Date: Sun, 1 Oct 2023 07:52:19 +0530 Subject: [PATCH 15/19] Program to find Second Min / Max --- src/main/java/com/thealgorithms/maths/SecondMinMax.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/maths/SecondMinMax.java b/src/main/java/com/thealgorithms/maths/SecondMinMax.java index e6620852b832..a3b622e5b472 100644 --- a/src/main/java/com/thealgorithms/maths/SecondMinMax.java +++ b/src/main/java/com/thealgorithms/maths/SecondMinMax.java @@ -27,7 +27,6 @@ private static int secondBest(final int[] arr, final int initialVal, final BiPre } checkOutput(secBest, initialVal); return secBest; - } /** From 9a52a123501a218fd15bbf2e80c17d6ac89e6850 Mon Sep 17 00:00:00 2001 From: BharathSanjeeviT Date: Sun, 1 Oct 2023 16:35:19 +0530 Subject: [PATCH 16/19] Program to find Second Minimum / Maximum --- src/main/java/com/thealgorithms/maths/SecondMinMax.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/SecondMinMax.java b/src/main/java/com/thealgorithms/maths/SecondMinMax.java index a3b622e5b472..a046dd55a810 100644 --- a/src/main/java/com/thealgorithms/maths/SecondMinMax.java +++ b/src/main/java/com/thealgorithms/maths/SecondMinMax.java @@ -46,11 +46,15 @@ public static int findSecondMax(final int[] arr) { } private static void checkInput(final int[] arr) { - if (arr.length < 2) throw new IllegalArgumentException("Input array must have length of at least two"); + 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"); + if (secNum == initialVal) { + throw new IllegalArgumentException("Input array should have at least 2 distinct elements"); + } return secNum; } } From c577f982cd9469a1f7418863d79edbbc1459ccf2 Mon Sep 17 00:00:00 2001 From: BharathSanjeeviT Date: Sun, 1 Oct 2023 16:46:31 +0530 Subject: [PATCH 17/19] Program to find Second Best Number --- src/main/java/com/thealgorithms/maths/SecondMinMax.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/SecondMinMax.java b/src/main/java/com/thealgorithms/maths/SecondMinMax.java index a046dd55a810..83ad273ed2da 100644 --- a/src/main/java/com/thealgorithms/maths/SecondMinMax.java +++ b/src/main/java/com/thealgorithms/maths/SecondMinMax.java @@ -51,10 +51,9 @@ private static void checkInput(final int[] arr) { } } - private static int checkOutput(final int secNum, int initialVal) { + private static void checkOutput(final int secNum, int initialVal) { if (secNum == initialVal) { throw new IllegalArgumentException("Input array should have at least 2 distinct elements"); } - return secNum; } } From 592bf7b8287016552f317bca4118bab5b906c972 Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Sun, 1 Oct 2023 17:21:01 +0200 Subject: [PATCH 18/19] style: mark `initialVal` as `final` --- src/main/java/com/thealgorithms/maths/SecondMinMax.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/maths/SecondMinMax.java b/src/main/java/com/thealgorithms/maths/SecondMinMax.java index 83ad273ed2da..3940c7b186e0 100644 --- a/src/main/java/com/thealgorithms/maths/SecondMinMax.java +++ b/src/main/java/com/thealgorithms/maths/SecondMinMax.java @@ -51,7 +51,7 @@ private static void checkInput(final int[] arr) { } } - private static void checkOutput(final int secNum, int initialVal) { + 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"); } From 56aeb71ed8e22d18142f8122eeabb6b7bb57f97b Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Sun, 1 Oct 2023 17:22:07 +0200 Subject: [PATCH 19/19] style: resolve `MultipleVariableDeclarations` Each variable declaration must be in its own statement. --- src/main/java/com/thealgorithms/maths/SecondMinMax.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/maths/SecondMinMax.java b/src/main/java/com/thealgorithms/maths/SecondMinMax.java index 3940c7b186e0..e5a2d3b89085 100644 --- a/src/main/java/com/thealgorithms/maths/SecondMinMax.java +++ b/src/main/java/com/thealgorithms/maths/SecondMinMax.java @@ -16,7 +16,8 @@ private SecondMinMax() { private static int secondBest(final int[] arr, final int initialVal, final BiPredicate isBetter) { checkInput(arr); - int best = initialVal, secBest = initialVal; + int best = initialVal; + int secBest = initialVal; for (final int num : arr) { if (isBetter.test(num, best)) { secBest = best;