Skip to content

Commit

Permalink
Merge pull request TheAlgorithms#984 from shellhub/dev
Browse files Browse the repository at this point in the history
AbsoluteMax and AbsoluteMin
  • Loading branch information
yanglbme authored Oct 8, 2019
2 parents 6209e59 + c68f4ca commit 53ee73f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Maths/AbsoluteMax.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package Maths;

import java.util.Arrays;

/**
* description:
* <p>
* absMax([0, 5, 1, 11]) = 11, absMax([3 , -10, -2]) = -10
* </p>
*/
public class AbsoluteMax {
public static void main(String[] args) {
int[] numbers = new int[]{3, -10, -2};
System.out.println("absMax(" + Arrays.toString(numbers) + ") = " + absMax(numbers));
}

/**
* get the value, it's absolute value is max
*
* @param numbers contains elements
* @return the absolute max value
*/
public static int absMax(int[] numbers) {
int absMaxValue = numbers[0];
for (int i = 1, length = numbers.length; i < length; ++i) {
if (Math.abs(numbers[i]) > Math.abs(absMaxValue)) {
absMaxValue = numbers[i];
}
}
return absMaxValue;
}
}
32 changes: 32 additions & 0 deletions Maths/AbsoluteMin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package Maths;

import java.util.Arrays;

/**
* description:
* <p>
* absMin([0, 5, 1, 11]) = 0, absMin([3 , -10, -2]) = -2
* </p>
*/
public class AbsoluteMin {
public static void main(String[] args) {
int[] numbers = new int[]{3, -10, -2};
System.out.println("absMin(" + Arrays.toString(numbers) + ") = " + absMin(numbers));
}

/**
* get the value, it's absolute value is min
*
* @param numbers contains elements
* @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];
}
}
return absMinValue;
}
}

0 comments on commit 53ee73f

Please sign in to comment.