Code refactor for AbsoluteMin improvements (#3022)

Fix #3021
This commit is contained in:
Cristiano Jesus
2022-04-20 09:12:55 +01:00
committed by GitHub
parent 719e1d8132
commit 02375e0683
2 changed files with 41 additions and 24 deletions

View File

@ -2,35 +2,31 @@ package com.thealgorithms.maths;
import java.util.Arrays;
/**
* description:
*
* <p>
* absMin([0, 5, 1, 11]) = 0, absMin([3 , -10, -2]) = -2
*/
public class AbsoluteMin {
public static void main(String[] args) {
int[] testnums = {4, 0, 16};
assert absMin(testnums) == 0;
int[] numbers = {3, -10, -2};
System.out.println("absMin(" + Arrays.toString(numbers) + ") = " + absMin(numbers));
}
/**
* get the value, returns the absolute min value min
* Compares the numbers given as arguments to get the absolute min value.
*
* @param numbers contains elements
* @return the absolute min value
* @param numbers The numbers to compare
* @return The absolute min value
*/
public static int absMin(int[] numbers) {
int absMinValue = numbers[0];
for (int i = 1, length = numbers.length; i < length; ++i) {
if (Math.abs(numbers[i]) < Math.abs(absMinValue)) {
absMinValue = numbers[i];
}
public static int getMinValue(int... numbers) {
if (numbers.length == 0) {
throw new IllegalArgumentException("Numbers array cannot be empty");
}
return absMinValue;
var absMinWrapper = new Object() {
int value = numbers[0];
};
Arrays.stream(numbers)
.skip(1)
.forEach(number -> {
if (Math.abs(number) < Math.abs(absMinWrapper.value)) {
absMinWrapper.value = number;
}
});
return absMinWrapper.value;
}
}