forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request TheAlgorithms#984 from shellhub/dev
AbsoluteMax and AbsoluteMin
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |