MCPcopy Create free account
hub / github.com/TheAlgorithms/Java / AbsoluteMax

Class AbsoluteMax

src/main/java/com/thealgorithms/maths/AbsoluteMax.java:3–26  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1package com.thealgorithms.maths;
2
3public final class AbsoluteMax {
4 private AbsoluteMax() {
5 }
6
7 /**
8 * Finds the absolute maximum value among the given numbers.
9 *
10 * @param numbers The numbers to compare.
11 * @return The absolute maximum value.
12 * @throws IllegalArgumentException If the input array is empty or null.
13 */
14 public static int getMaxValue(int... numbers) {
15 if (numbers == null || numbers.length == 0) {
16 throw new IllegalArgumentException("Numbers array cannot be empty or null");
17 }
18 int absMax = numbers[0];
19 for (int i = 1; i < numbers.length; i++) {
20 if (Math.abs(numbers[i]) > Math.abs(absMax) || (Math.abs(numbers[i]) == Math.abs(absMax) && numbers[i] > absMax)) {
21 absMax = numbers[i];
22 }
23 }
24 return absMax;
25 }
26}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected