Skip to content

Commit 1be07c9

Browse files
committed
Add Sliding Window algorithm and tests for maximum sum of subarray
1 parent fed2d4b commit 1be07c9

File tree

2 files changed

+122
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)