Repository: TheAlgorithms/Java
Pull request #5904 (2024-10-19 16:15:34)

Add edge case to handle negative rod length in RodCutting algorithm
<!--
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
-->

<!-- 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`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5904?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `0%` with `2 lines` in your changes missing coverage. Please review.
> Project coverage is 66.79%. Comparing base [(`e499d3b`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/e499d3b63e9cb3dc711ddd5d4a2fc3faba9b3d46?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`c6e01a8`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/c6e01a8d93e8971a672348cbd9b2bab0d174fd95?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5904?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...m/thealgorithms/dynamicprogramming/RodCutting.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5904?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fdynamicprogramming%2FRodCutting.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9keW5hbWljcHJvZ3JhbW1pbmcvUm9kQ3V0dGluZy5qYXZh) | 0.00% | [1 Missing and 1 partial :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5904?src=pr&el=tree&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    #5904      +/-   ##
============================================
- Coverage     66.80%   66.79%   -0.01%     
  Complexity     4501     4501              
============================================
  Files           611      611              
  Lines         16958    16960       +2     
  Branches       3275     3276       +1     
============================================
  Hits          11328    11328              
- Misses         5182     5183       +1     
- Partials        448      449       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5904?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).

Chiefpatwal: I’ve made some updates to the RodCutting algorithm to handle an edge case where the length of the rod is negative. Previously, the code didn’t account for this situation, which could lead to unexpected behavior.

Now, I’ve added an IllegalArgumentException to ensure that if a negative rod length is passed, it throws a clear error message, preventing any further calculations. This change should enhance the robustness of the algorithm.

Additionally, I've implemented a test case for this scenario, and it’s working correctly to validate the expected behavior.

Patch:
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java
index 76b341e2..6d33826b 100644
--- a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java
@@ -22,6 +22,10 @@ public final class RodCutting {
         if (price == null || price.length == 0) {
             throw new IllegalArgumentException("Price array cannot be null or empty.");
         }
+        if (n < 0) {
+            throw new IllegalArgumentException("Rod length cannot be negative.");
+        }
+
         // Create an array to store the maximum obtainable values for each rod length.
         int[] val = new int[n + 1];
         val[0] = 0;


Test patch:
diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java
index 39497a76..9cf21fd8 100644
--- a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java
+++ b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java
@@ -93,4 +93,10 @@ class RodCuttingTest {
         int length = 5;
         assertThrows(IllegalArgumentException.class, () -> RodCutting.cutRod(prices, length), "An empty prices array should throw an IllegalArgumentException.");
     }
+    @Test
+    void testCutRodNegativeLength() {
+        int[] prices = {1, 5, 8, 9, 10}; // Prices are irrelevant for negative length
+        int length = -1;
+        assertThrows(IllegalArgumentException.class, () -> RodCutting.cutRod(prices, length), "A negative rod length should throw an IllegalArgumentException.");
+    }
 }