Add MinSumKSizeSubarray algorithm (#6025)

This commit is contained in:
Rashi Dashore
2024-10-26 20:24:53 +05:30
committed by GitHub
parent 4d85c61c37
commit 03777f8d88
2 changed files with 130 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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));
}
}