Skip to content

Commit 6b5b563

Browse files
committed
Refactor Sliding Window algorithm implementation and tests
1 parent 59dd494 commit 6b5b563

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/main/java/com/thealgorithms/slidingwindow/maxSumKSizeSubarray.java renamed to src/main/java/com/thealgorithms/slidingwindow/MaxSumKSizeSubarray.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
* @author Your Name (https://github.com/Chiefpatwal)
1414
*/
15-
class MaxSumKSizeSubarray {
15+
public class MaxSumKSizeSubarray {
1616

1717
// Prevent instantiation
1818
private MaxSumKSizeSubarray() {
@@ -22,15 +22,15 @@ private MaxSumKSizeSubarray() {
2222
* This method finds the maximum sum of a subarray of a given size k.
2323
*
2424
* @param arr is the input array where the maximum sum needs to be found
25-
* @param k is the size of the subarray
25+
* @param k is the size of the subarray
2626
* @return the maximum sum of the subarray of size k
2727
*/
28-
public static int max_sum_k_size_subarray(int[] arr, int k) {
28+
public static int maxSumKSizeSubarray(int[] arr, int k) {
2929
if (arr.length < k) {
3030
return -1; // Edge case: not enough elements
3131
}
3232

33-
int maxSum = 0;
33+
int maxSum ;
3434
int windowSum = 0;
3535

3636
// Calculate the sum of the first window

src/test/java/com/thealgorithms/slidingwindow/maxSumKSizeSubarrayTest.java renamed to src/test/java/com/thealgorithms/slidingwindow/MaxSumKSizeSubarrayTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import org.junit.jupiter.api.Test;
66

77
/**
8-
* Unit tests for the SlidingWindow class.
8+
* Unit tests for the MaxSumKSizeSubarray class.
99
*
1010
* @author Your Name (https://github.com/Chiefpatwal)
1111
*/
12-
class SlidingWindowTest {
12+
class MaxSumKSizeSubarrayTest {
1313

1414
/**
1515
* Test for the basic case of finding the maximum sum.
@@ -19,7 +19,7 @@ void testMaxSumKSizeSubarray() {
1919
int[] arr = {1, 2, 3, 4, 5};
2020
int k = 2;
2121
int expectedMaxSum = 9; // 4 + 5
22-
assertEquals(expectedMaxSum, SlidingWindow.maxSumKSizeSubarray(arr, k));
22+
assertEquals(expectedMaxSum, MaxSumKSizeSubarray.maxSumKSizeSubarray(arr, k));
2323
}
2424

2525
/**
@@ -30,7 +30,7 @@ void testMaxSumKSizeSubarrayWithDifferentValues() {
3030
int[] arr = {2, 1, 5, 1, 3, 2};
3131
int k = 3;
3232
int expectedMaxSum = 9; // 5 + 1 + 3
33-
assertEquals(expectedMaxSum, SlidingWindow.maxSumKSizeSubarray(arr, k));
33+
assertEquals(expectedMaxSum, MaxSumKSizeSubarray.maxSumKSizeSubarray(arr, k));
3434
}
3535

3636
/**
@@ -41,7 +41,7 @@ void testMaxSumKSizeSubarrayWithInsufficientElements() {
4141
int[] arr = {1, 2};
4242
int k = 3; // Not enough elements
4343
int expectedMaxSum = -1; // Edge case
44-
assertEquals(expectedMaxSum, SlidingWindow.maxSumKSizeSubarray(arr, k));
44+
assertEquals(expectedMaxSum, MaxSumKSizeSubarray.maxSumKSizeSubarray(arr, k));
4545
}
4646

4747
/**
@@ -52,7 +52,7 @@ void testMaxSumKSizeSubarrayWithLargeArray() {
5252
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
5353
int k = 5;
5454
int expectedMaxSum = 40; // 6 + 7 + 8 + 9 + 10
55-
assertEquals(expectedMaxSum, SlidingWindow.maxSumKSizeSubarray(arr, k));
55+
assertEquals(expectedMaxSum, MaxSumKSizeSubarray.maxSumKSizeSubarray(arr, k));
5656
}
5757

5858
/**
@@ -63,7 +63,7 @@ void testMaxSumKSizeSubarrayWithNegativeNumbers() {
6363
int[] arr = {-1, -2, -3, -4, -5};
6464
int k = 2;
6565
int expectedMaxSum = -3; // -1 + -2
66-
assertEquals(expectedMaxSum, SlidingWindow.maxSumKSizeSubarray(arr, k));
66+
assertEquals(expectedMaxSum, MaxSumKSizeSubarray.maxSumKSizeSubarray(arr, k));
6767
}
6868

6969
/**
@@ -74,6 +74,6 @@ void testMaxSumKSizeSubarrayWithKEqualToArrayLength() {
7474
int[] arr = {1, 2, 3, 4, 5};
7575
int k = 5;
7676
int expectedMaxSum = 15; // 1 + 2 + 3 + 4 + 5
77-
assertEquals(expectedMaxSum, SlidingWindow.maxSumKSizeSubarray(arr, k));
77+
assertEquals(expectedMaxSum, MaxSumKSizeSubarray.maxSumKSizeSubarray(arr, k));
7878
}
7979
}

0 commit comments

Comments
 (0)