File tree Expand file tree Collapse file tree
main/java/com/thealgorithms/maths
test/java/com/thealgorithms/maths Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -15,12 +15,13 @@ public static int getMinValue(int... numbers) {
1515 throw new IllegalArgumentException ("Numbers array cannot be empty or null" );
1616 }
1717
18- int absMin = numbers [0 ];
18+ long absMin = numbers [0 ];
1919 for (int i = 1 ; i < numbers .length ; i ++) {
20- if (Math .abs (numbers [i ]) < Math .abs (absMin ) || (Math .abs (numbers [i ]) == Math .abs (absMin ) && numbers [i ] < absMin )) {
21- absMin = numbers [i ];
20+ long current = numbers [i ];
21+ if (Math .abs (current ) < Math .abs (absMin ) || (Math .abs (current ) == Math .abs (absMin ) && current < absMin )) {
22+ absMin = current ;
2223 }
2324 }
24- return absMin ;
25+ return ( int ) absMin ;
2526 }
2627}
Original file line number Diff line number Diff line change @@ -27,4 +27,13 @@ void testGetMinValueWithSameAbsoluteValues() {
2727 assertEquals (-5 , AbsoluteMin .getMinValue (-5 , 5 ));
2828 assertEquals (-5 , AbsoluteMin .getMinValue (5 , -5 ));
2929 }
30+
31+ @ Test
32+ void testIntegerMinValueOverflow () {
33+ assertEquals (1 , AbsoluteMin .getMinValue (Integer .MIN_VALUE , 1 ));
34+ assertEquals (-1 , AbsoluteMin .getMinValue (Integer .MIN_VALUE , -1 ));
35+ assertEquals (0 , AbsoluteMin .getMinValue (Integer .MIN_VALUE , 0 ));
36+ assertEquals (Integer .MIN_VALUE , AbsoluteMin .getMinValue (Integer .MIN_VALUE ));
37+ assertEquals (Integer .MAX_VALUE , AbsoluteMin .getMinValue (Integer .MIN_VALUE , Integer .MAX_VALUE ));
38+ }
3039}
You can’t perform that action at this time.
0 commit comments