Repository: TheAlgorithms/Java
Pull request #6025 (2024-10-26 12:11:50)

feature: Adding MinSumKSizeSubarray in Sliding Window 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/6025?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 `10 lines` in your changes missing coverage. Please review.
> Project coverage is 70.48%. Comparing base [(`f20f8d1`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/f20f8d18360c074b1ea70e3760ca90a8651a3f7e?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`c7b61e6`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/c7b61e682caed0674a2f9b96bf9f347069133895?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).
> Report is 1 commits behind head on master.

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6025?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...ealgorithms/slidingwindow/MinSumKSizeSubarray.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6025?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fslidingwindow%2FMinSumKSizeSubarray.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9zbGlkaW5nd2luZG93L01pblN1bUtTaXplU3ViYXJyYXkuamF2YQ==) | 0.00% | [10 Missing :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6025?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    #6025      +/-   ##
============================================
- Coverage     70.52%   70.48%   -0.05%     
  Complexity     4820     4820              
============================================
  Files           644      645       +1     
  Lines         17458    17468      +10     
  Branches       3359     3361       +2     
============================================
  Hits          12313    12313              
- Misses         4681     4691      +10     
  Partials        464      464              
```

</details>

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

rashi07dashore: > Please add JUnit tests

@siriak please check. Thank you~

Patch:
diff --git a/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java b/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java
new file mode 100644
index 00000000..40a5441f
--- /dev/null
+++ b/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java
@@ -0,0 +1,51 @@
+package com.thealgorithms.slidingwindow;
+/**
+ * The Sliding Window algorithm is used to find the minimum sum of a subarray
+ * of a fixed size k within a given array.
+ *
+ * <p>
+ * Worst-case performance O(n)
+ * Best-case performance O(n)
+ * Average performance O(n)
+ * Worst-case space complexity O(1)
+ *
+ * This class provides a static method to find the minimum sum of a subarray
+ * with a specified length k.
+ *
+ * @author Rashi Dashore (https://github.com/rashi07dashore)
+ */
+public final class MinSumKSizeSubarray {
+
+    // Prevent instantiation
+    private MinSumKSizeSubarray() {
+    }
+
+    /**
+     * This method finds the minimum sum of a subarray of a given size k.
+     *
+     * @param arr is the input array where the minimum sum needs to be found
+     * @param k   is the size of the subarray
+     * @return the minimum sum of the subarray of size k
+     */
+    public static int minSumKSizeSubarray(int[] arr, int k) {
+        if (arr.length < k) {
+            return -1; // Edge case: not enough elements
+        }
+
+        int minSum;
+        int windowSum = 0;
+
+        // Calculate the sum of the first window
+        for (int i = 0; i < k; i++) {
+            windowSum += arr[i];
+        }
+        minSum = windowSum;
+
+        // Slide the window across the array
+        for (int i = k; i < arr.length; i++) {
+            windowSum += arr[i] - arr[i - k];
+            minSum = Math.min(minSum, windowSum);
+        }
+        return minSum;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarrayTest.java b/src/test/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarrayTest.java
new file mode 100644
index 00000000..4656c8ba
--- /dev/null
+++ b/src/test/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarrayTest.java
@@ -0,0 +1,79 @@
+package com.thealgorithms.slidingwindow;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Unit tests for the MinSumKSizeSubarray class.
+ *
+ * @author Rashi Dashore (https://github.com/rashi07dashore)
+ */
+class MinSumKSizeSubarrayTest {
+
+    /**
+     * Test for the basic case of finding the minimum sum.
+     */
+    @Test
+    void testMinSumKSizeSubarray() {
+        int[] arr = {2, 1, 5, 1, 3, 2};
+        int k = 3;
+        int expectedMinSum = 6; // Corrected: Minimum sum of a subarray of size 3
+        assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k));
+    }
+
+    /**
+     * Test for a different array and subarray size.
+     */
+    @Test
+    void testMinSumKSizeSubarrayWithDifferentValues() {
+        int[] arr = {1, 2, 3, 4, 5};
+        int k = 2;
+        int expectedMinSum = 3; // 1 + 2
+        assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k));
+    }
+
+    /**
+     * Test for edge case with insufficient elements.
+     */
+    @Test
+    void testMinSumKSizeSubarrayWithInsufficientElements() {
+        int[] arr = {1, 2};
+        int k = 3; // Not enough elements
+        int expectedMinSum = -1; // Edge case
+        assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k));
+    }
+
+    /**
+     * Test for large array.
+     */
+    @Test
+    void testMinSumKSizeSubarrayWithLargeArray() {
+        int[] arr = {5, 4, 3, 2, 1, 0, -1, -2, -3, -4};
+        int k = 5;
+        int expectedMinSum = -10; // -1 + -2 + -3 + -4 + 0
+        assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k));
+    }
+
+    /**
+     * Test for array with negative numbers.
+     */
+    @Test
+    void testMinSumKSizeSubarrayWithNegativeNumbers() {
+        int[] arr = {-1, -2, -3, -4, -5};
+        int k = 2;
+        int expectedMinSum = -9; // -4 + -5
+        assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k));
+    }
+
+    /**
+     * Test for the case where k equals the array length.
+     */
+    @Test
+    void testMinSumKSizeSubarrayWithKEqualToArrayLength() {
+        int[] arr = {1, 2, 3, 4, 5};
+        int k = 5;
+        int expectedMinSum = 15; // 1 + 2 + 3 + 4 + 5
+        assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k));
+    }
+}