File tree 1 file changed +6
-16
lines changed
src/main/java/com/thealgorithms/maths
1 file changed +6
-16
lines changed Original file line number Diff line number Diff line change 1
- package com .thealgorithms .maths ;
2
-
3
1
public final class AbsoluteMax {
4
- private AbsoluteMax () {
5
- }
2
+ private AbsoluteMax () {}
6
3
7
4
/**
8
5
* Finds the absolute maximum value among the given numbers.
@@ -15,18 +12,11 @@ public static int getMaxValue(int... numbers) {
15
12
if (numbers == null || numbers .length == 0 ) {
16
13
throw new IllegalArgumentException ("Numbers array cannot be empty or null" );
17
14
}
18
-
19
- int maxPositive = Integer .MIN_VALUE ;
20
- int maxNegative = Integer .MIN_VALUE ;
21
-
15
+
16
+ int max = Integer .MIN_VALUE ;
22
17
for (int number : numbers ) {
23
- if (number >= 0 && number > maxPositive ) {
24
- maxPositive = number ;
25
- } else if (number < 0 && Math .abs (number ) > Math .abs (maxNegative )) {
26
- maxNegative = number ;
27
- }
18
+ max = Math .max (max , Math .abs (number ));
28
19
}
29
-
30
- return Math .max (maxPositive , maxNegative );
20
+ return max ;
31
21
}
32
- }
22
+ }
You can’t perform that action at this time.
0 commit comments