forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbsoluteMin.java
More file actions
27 lines (24 loc) · 830 Bytes
/
Copy pathAbsoluteMin.java
File metadata and controls
27 lines (24 loc) · 830 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
25
26
27
package com.thealgorithms.maths;
public final class AbsoluteMin {
private AbsoluteMin() {
}
/**
* Compares the numbers given as arguments to get the absolute min value.
*
* @param numbers The numbers to compare
* @return The absolute min value
*/
public static int getMinValue(int... numbers) {
if (numbers == null || numbers.length == 0) {
throw new IllegalArgumentException("Numbers array cannot be empty or null");
}
long absMin = numbers[0];
for (int i = 1; i < numbers.length; i++) {
long current = numbers[i];
if (Math.abs(current) < Math.abs(absMin) || (Math.abs(current) == Math.abs(absMin) && current < absMin)) {
absMin = current;
}
}
return (int) absMin;
}
}