- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathAbsoluteMax.java
26 lines (24 loc) · 865 Bytes
/
AbsoluteMax.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
packagecom.thealgorithms.maths;
publicfinalclassAbsoluteMax {
privateAbsoluteMax() {
}
/**
* Finds the absolute maximum value among the given numbers.
*
* @param numbers The numbers to compare.
* @return The absolute maximum value.
* @throws IllegalArgumentException If the input array is empty or null.
*/
publicstaticintgetMaxValue(int... numbers) {
if (numbers == null || numbers.length == 0) {
thrownewIllegalArgumentException("Numbers array cannot be empty or null");
}
intabsMax = numbers[0];
for (inti = 1; i < numbers.length; i++) {
if (Math.abs(numbers[i]) > Math.abs(absMax) || (Math.abs(numbers[i]) == Math.abs(absMax) && numbers[i] > absMax)) {
absMax = numbers[i];
}
}
returnabsMax;
}
}