Skip to content

Commit 53ee73f

Browse files
authored
Merge pull request TheAlgorithms#984 from shellhub/dev
AbsoluteMax and AbsoluteMin
2 parents 6209e59 + c68f4ca commit 53ee73f

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

Maths/AbsoluteMax.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package Maths;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* description:
7+
* <p>
8+
* absMax([0, 5, 1, 11]) = 11, absMax([3 , -10, -2]) = -10
9+
* </p>
10+
*/
11+
public class AbsoluteMax {
12+
public static void main(String[] args) {
13+
int[] numbers = new int[]{3, -10, -2};
14+
System.out.println("absMax(" + Arrays.toString(numbers) + ") = " + absMax(numbers));
15+
}
16+
17+
/**
18+
* get the value, it's absolute value is max
19+
*
20+
* @param numbers contains elements
21+
* @return the absolute max value
22+
*/
23+
public static int absMax(int[] numbers) {
24+
int absMaxValue = numbers[0];
25+
for (int i = 1, length = numbers.length; i < length; ++i) {
26+
if (Math.abs(numbers[i]) > Math.abs(absMaxValue)) {
27+
absMaxValue = numbers[i];
28+
}
29+
}
30+
return absMaxValue;
31+
}
32+
}

Maths/AbsoluteMin.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package Maths;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* description:
7+
* <p>
8+
* absMin([0, 5, 1, 11]) = 0, absMin([3 , -10, -2]) = -2
9+
* </p>
10+
*/
11+
public class AbsoluteMin {
12+
public static void main(String[] args) {
13+
int[] numbers = new int[]{3, -10, -2};
14+
System.out.println("absMin(" + Arrays.toString(numbers) + ") = " + absMin(numbers));
15+
}
16+
17+
/**
18+
* get the value, it's absolute value is min
19+
*
20+
* @param numbers contains elements
21+
* @return the absolute min value
22+
*/
23+
public static int absMin(int[] numbers) {
24+
int absMinValue = numbers[0];
25+
for (int i = 1, length = numbers.length; i < length; ++i) {
26+
if (Math.abs(numbers[i]) < Math.abs(absMinValue)) {
27+
absMinValue = numbers[i];
28+
}
29+
}
30+
return absMinValue;
31+
}
32+
}

0 commit comments

Comments
 (0)