Skip to content

AbsoluteMax and AbsoluteMin #984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}