Skip to content

Commit bc9645c

Browse files
authored
Add Sliding Window algorithm and tests for maximum sum of subarray (#6001)
1 parent 202879a commit bc9645c

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.thealgorithms.slidingwindow;
2+
3+
/**
4+
* The Sliding Window algorithm is used to find the maximum sum of a subarray
5+
* of a fixed size k within a given array.
6+
*
7+
* <p>
8+
* Worst-case performance O(n)
9+
* Best-case performance O(n)
10+
* Average performance O(n)
11+
* Worst-case space complexity O(1)
12+
*
13+
* @author Your Name (https://github.com/Chiefpatwal)
14+
*/
15+
public final class MaxSumKSizeSubarray {
16+
17+
// Prevent instantiation
18+
private MaxSumKSizeSubarray() {
19+
}
20+
21+
/**
22+
* This method finds the maximum sum of a subarray of a given size k.
23+
*
24+
* @param arr is the input array where the maximum sum needs to be found
25+
* @param k is the size of the subarray
26+
* @return the maximum sum of the subarray of size k
27+
*/
28+
public static int maxSumKSizeSubarray(int[] arr, int k) {
29+
if (arr.length < k) {
30+
return -1; // Edge case: not enough elements
31+
}
32+
33+
int maxSum;
34+
int windowSum = 0;
35+
36+
// Calculate the sum of the first window
37+
for (int i = 0; i < k; i++) {
38+
windowSum += arr[i];
39+
}
40+
maxSum = windowSum;
41+
42+
// Slide the window across the array
43+
for (int i = k; i < arr.length; i++) {
44+
windowSum += arr[i] - arr[i - k];
45+
maxSum = Math.max(maxSum, windowSum);
46+
}
47+
48+
return maxSum;
49+
}
50+
}
Lines changed: 79 additions & 0 deletions
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 MaxSumKSizeSubarray class.
9+
*
10+
* @author Your Name (https://github.com/Chiefpatwal)
11+
*/
12+
class MaxSumKSizeSubarrayTest {
13+
14+
/**
15+
* Test for the basic case of finding the maximum sum.
16+
*/
17+
@Test
18+
void testMaxSumKSizeSubarray() {
19+
int[] arr = {1, 2, 3, 4, 5};
20+
int k = 2;
21+
int expectedMaxSum = 9; // 4 + 5
22+
assertEquals(expectedMaxSum, MaxSumKSizeSubarray.maxSumKSizeSubarray(arr, k));
23+
}
24+
25+
/**
26+
* Test for a different array and subarray size.
27+
*/
28+
@Test
29+
void testMaxSumKSizeSubarrayWithDifferentValues() {
30+
int[] arr = {2, 1, 5, 1, 3, 2};
31+
int k = 3;
32+
int expectedMaxSum = 9; // 5 + 1 + 3
33+
assertEquals(expectedMaxSum, MaxSumKSizeSubarray.maxSumKSizeSubarray(arr, k));
34+
}
35+
36+
/**
37+
* Test for edge case with insufficient elements.
38+
*/
39+
@Test
40+
void testMaxSumKSizeSubarrayWithInsufficientElements() {
41+
int[] arr = {1, 2};
42+
int k = 3; // Not enough elements
43+
int expectedMaxSum = -1; // Edge case
44+
assertEquals(expectedMaxSum, MaxSumKSizeSubarray.maxSumKSizeSubarray(arr, k));
45+
}
46+
47+
/**
48+
* Test for large array.
49+
*/
50+
@Test
51+
void testMaxSumKSizeSubarrayWithLargeArray() {
52+
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
53+
int k = 5;
54+
int expectedMaxSum = 40; // 6 + 7 + 8 + 9 + 10
55+
assertEquals(expectedMaxSum, MaxSumKSizeSubarray.maxSumKSizeSubarray(arr, k));
56+
}
57+
58+
/**
59+
* Test for array with negative numbers.
60+
*/
61+
@Test
62+
void testMaxSumKSizeSubarrayWithNegativeNumbers() {
63+
int[] arr = {-1, -2, -3, -4, -5};
64+
int k = 2;
65+
int expectedMaxSum = -3; // -1 + -2
66+
assertEquals(expectedMaxSum, MaxSumKSizeSubarray.maxSumKSizeSubarray(arr, k));
67+
}
68+
69+
/**
70+
* Test for the case where k equals the array length.
71+
*/
72+
@Test
73+
void testMaxSumKSizeSubarrayWithKEqualToArrayLength() {
74+
int[] arr = {1, 2, 3, 4, 5};
75+
int k = 5;
76+
int expectedMaxSum = 15; // 1 + 2 + 3 + 4 + 5
77+
assertEquals(expectedMaxSum, MaxSumKSizeSubarray.maxSumKSizeSubarray(arr, k));
78+
}
79+
}

0 commit comments

Comments
 (0)