Skip to content

Commit 03777f8

Browse files
Add MinSumKSizeSubarray algorithm (#6025)
1 parent 4d85c61 commit 03777f8

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.thealgorithms.slidingwindow;
2+
/**
3+
* The Sliding Window algorithm is used to find the minimum sum of a subarray
4+
* of a fixed size k within a given array.
5+
*
6+
* <p>
7+
* Worst-case performance O(n)
8+
* Best-case performance O(n)
9+
* Average performance O(n)
10+
* Worst-case space complexity O(1)
11+
*
12+
* This class provides a static method to find the minimum sum of a subarray
13+
* with a specified length k.
14+
*
15+
* @author Rashi Dashore (https://github.com/rashi07dashore)
16+
*/
17+
public final class MinSumKSizeSubarray {
18+
19+
// Prevent instantiation
20+
private MinSumKSizeSubarray() {
21+
}
22+
23+
/**
24+
* This method finds the minimum sum of a subarray of a given size k.
25+
*
26+
* @param arr is the input array where the minimum sum needs to be found
27+
* @param k is the size of the subarray
28+
* @return the minimum sum of the subarray of size k
29+
*/
30+
public static int minSumKSizeSubarray(int[] arr, int k) {
31+
if (arr.length < k) {
32+
return -1; // Edge case: not enough elements
33+
}
34+
35+
int minSum;
36+
int windowSum = 0;
37+
38+
// Calculate the sum of the first window
39+
for (int i = 0; i < k; i++) {
40+
windowSum += arr[i];
41+
}
42+
minSum = windowSum;
43+
44+
// Slide the window across the array
45+
for (int i = k; i < arr.length; i++) {
46+
windowSum += arr[i] - arr[i - k];
47+
minSum = Math.min(minSum, windowSum);
48+
}
49+
return minSum;
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.thealgorithms.slidingwindow;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
/**
8+
* Unit tests for the MinSumKSizeSubarray class.
9+
*
10+
* @author Rashi Dashore (https://github.com/rashi07dashore)
11+
*/
12+
class MinSumKSizeSubarrayTest {
13+
14+
/**
15+
* Test for the basic case of finding the minimum sum.
16+
*/
17+
@Test
18+
void testMinSumKSizeSubarray() {
19+
int[] arr = {2, 1, 5, 1, 3, 2};
20+
int k = 3;
21+
int expectedMinSum = 6; // Corrected: Minimum sum of a subarray of size 3
22+
assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k));
23+
}
24+
25+
/**
26+
* Test for a different array and subarray size.
27+
*/
28+
@Test
29+
void testMinSumKSizeSubarrayWithDifferentValues() {
30+
int[] arr = {1, 2, 3, 4, 5};
31+
int k = 2;
32+
int expectedMinSum = 3; // 1 + 2
33+
assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k));
34+
}
35+
36+
/**
37+
* Test for edge case with insufficient elements.
38+
*/
39+
@Test
40+
void testMinSumKSizeSubarrayWithInsufficientElements() {
41+
int[] arr = {1, 2};
42+
int k = 3; // Not enough elements
43+
int expectedMinSum = -1; // Edge case
44+
assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k));
45+
}
46+
47+
/**
48+
* Test for large array.
49+
*/
50+
@Test
51+
void testMinSumKSizeSubarrayWithLargeArray() {
52+
int[] arr = {5, 4, 3, 2, 1, 0, -1, -2, -3, -4};
53+
int k = 5;
54+
int expectedMinSum = -10; // -1 + -2 + -3 + -4 + 0
55+
assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k));
56+
}
57+
58+
/**
59+
* Test for array with negative numbers.
60+
*/
61+
@Test
62+
void testMinSumKSizeSubarrayWithNegativeNumbers() {
63+
int[] arr = {-1, -2, -3, -4, -5};
64+
int k = 2;
65+
int expectedMinSum = -9; // -4 + -5
66+
assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k));
67+
}
68+
69+
/**
70+
* Test for the case where k equals the array length.
71+
*/
72+
@Test
73+
void testMinSumKSizeSubarrayWithKEqualToArrayLength() {
74+
int[] arr = {1, 2, 3, 4, 5};
75+
int k = 5;
76+
int expectedMinSum = 15; // 1 + 2 + 3 + 4 + 5
77+
assertEquals(expectedMinSum, MinSumKSizeSubarray.minSumKSizeSubarray(arr, k));
78+
}
79+
}

0 commit comments

Comments
 (0)