forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbsoluteMax.java
More file actions
24 lines (22 loc) · 756 Bytes
/
AbsoluteMax.java
File metadata and controls
24 lines (22 loc) · 756 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.thealgorithms.maths;
public class AbsoluteMax {
/**
* 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.
*/
public static int getMaxValue(int... numbers) {
if (numbers == null || numbers.length == 0) {
throw new IllegalArgumentException("Numbers array cannot be empty or null");
}
int absMax = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (Math.abs(numbers[i]) > Math.abs(absMax)) {
absMax = numbers[i];
}
}
return absMax;
}
}