Repository: TheAlgorithms/Java
Pull request #6144 (2025-01-18 18:34:21)

Fix absolute max bug
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

The AbsoluteMax class contains a bug in the getMaxValue method. When two numbers have the same absolute value (e.g., 5 and -5), the method returns the number that appears later in the array. This behavior is inconsistent and may lead to unexpected results.

Explanation with Example.

Before Fix
5, -5 returns -5 
-5, 5 returns  5

After Fix
5, -5 returns  5 
-5, 5 returns  5

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

Thanks

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6144?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 73.82%. Comparing base [(`5454e2f`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/5454e2ff626547eaac3edad1d1459f6bf5ca7e7f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`19b8975`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/19b897546aee5a376a18f4e4d21ea09da1165a62?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #6144      +/-   ##
============================================
- Coverage     73.84%   73.82%   -0.02%     
- Complexity     5154     5155       +1     
============================================
  Files           661      661              
  Lines         17687    17687              
  Branches       3409     3409              
============================================
- Hits          13061    13058       -3     
- Misses         4119     4120       +1     
- Partials        507      509       +2     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6144?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/maths/AbsoluteMax.java b/src/main/java/com/thealgorithms/maths/AbsoluteMax.java
index d0c3db37..c32a408b 100644
--- a/src/main/java/com/thealgorithms/maths/AbsoluteMax.java
+++ b/src/main/java/com/thealgorithms/maths/AbsoluteMax.java
@@ -17,7 +17,7 @@ public final class AbsoluteMax {
         }
         int absMax = numbers[0];
         for (int i = 1; i < numbers.length; i++) {
-            if (Math.abs(numbers[i]) > Math.abs(absMax)) {
+            if (Math.abs(numbers[i]) > Math.abs(absMax) || (Math.abs(numbers[i]) == Math.abs(absMax) && numbers[i] > absMax)) {
                 absMax = numbers[i];
             }
         }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/AbsoluteMaxTest.java b/src/test/java/com/thealgorithms/maths/AbsoluteMaxTest.java
index 70d2f64b..33461fbb 100644
--- a/src/test/java/com/thealgorithms/maths/AbsoluteMaxTest.java
+++ b/src/test/java/com/thealgorithms/maths/AbsoluteMaxTest.java
@@ -19,4 +19,12 @@ public class AbsoluteMaxTest {
     void testGetMaxValueWithNoArguments() {
         assertThrows(IllegalArgumentException.class, AbsoluteMax::getMaxValue);
     }
+
+    @Test
+    void testGetMaxValueWithSameAbsoluteValues() {
+        assertEquals(5, AbsoluteMax.getMaxValue(-5, 5));
+        assertEquals(5, AbsoluteMax.getMaxValue(5, -5));
+        assertEquals(12, AbsoluteMax.getMaxValue(-12, 9, 3, 12, 1));
+        assertEquals(12, AbsoluteMax.getMaxValue(12, 9, 3, -12, 1));
+    }
 }