Skip to content

Add Sliding Window algorithm and tests for maximum sum of subarray #6001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 25, 2024
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.thealgorithms.slidingwindow;

/**
* The Sliding Window algorithm is used to find the maximum 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)
*
* @author Your Name (https://github.com/Chiefpatwal)
*/
public final class MaxSumKSizeSubarray {

// Prevent instantiation
private MaxSumKSizeSubarray() {
}

/**
* This method finds the maximum sum of a subarray of a given size k.
*
* @param arr is the input array where the maximum sum needs to be found
* @param k is the size of the subarray
* @return the maximum sum of the subarray of size k
*/
public static int maxSumKSizeSubarray(int[] arr, int k) {
if (arr.length < k) {
return -1; // Edge case: not enough elements
}

int maxSum;
int windowSum = 0;

// Calculate the sum of the first window
for (int i = 0; i < k; i++) {
windowSum += arr[i];
}
maxSum = windowSum;

// Slide the window across the array
for (int i = k; i < arr.length; i++) {
windowSum += arr[i] - arr[i - k];
maxSum = Math.max(maxSum, windowSum);
}

return maxSum;
}
}
Original file line number Diff line number Diff line change
@@ -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 MaxSumKSizeSubarray class.
*
* @author Your Name (https://github.com/Chiefpatwal)
*/
class MaxSumKSizeSubarrayTest {

/**
* Test for the basic case of finding the maximum sum.
*/
@Test
void testMaxSumKSizeSubarray() {
int[] arr = {1, 2, 3, 4, 5};
int k = 2;
int expectedMaxSum = 9; // 4 + 5
assertEquals(expectedMaxSum, MaxSumKSizeSubarray.maxSumKSizeSubarray(arr, k));
}

/**
* Test for a different array and subarray size.
*/
@Test
void testMaxSumKSizeSubarrayWithDifferentValues() {
int[] arr = {2, 1, 5, 1, 3, 2};
int k = 3;
int expectedMaxSum = 9; // 5 + 1 + 3
assertEquals(expectedMaxSum, MaxSumKSizeSubarray.maxSumKSizeSubarray(arr, k));
}

/**
* Test for edge case with insufficient elements.
*/
@Test
void testMaxSumKSizeSubarrayWithInsufficientElements() {
int[] arr = {1, 2};
int k = 3; // Not enough elements
int expectedMaxSum = -1; // Edge case
assertEquals(expectedMaxSum, MaxSumKSizeSubarray.maxSumKSizeSubarray(arr, k));
}

/**
* Test for large array.
*/
@Test
void testMaxSumKSizeSubarrayWithLargeArray() {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int k = 5;
int expectedMaxSum = 40; // 6 + 7 + 8 + 9 + 10
assertEquals(expectedMaxSum, MaxSumKSizeSubarray.maxSumKSizeSubarray(arr, k));
}

/**
* Test for array with negative numbers.
*/
@Test
void testMaxSumKSizeSubarrayWithNegativeNumbers() {
int[] arr = {-1, -2, -3, -4, -5};
int k = 2;
int expectedMaxSum = -3; // -1 + -2
assertEquals(expectedMaxSum, MaxSumKSizeSubarray.maxSumKSizeSubarray(arr, k));
}

/**
* Test for the case where k equals the array length.
*/
@Test
void testMaxSumKSizeSubarrayWithKEqualToArrayLength() {
int[] arr = {1, 2, 3, 4, 5};
int k = 5;
int expectedMaxSum = 15; // 1 + 2 + 3 + 4 + 5
assertEquals(expectedMaxSum, MaxSumKSizeSubarray.maxSumKSizeSubarray(arr, k));
}
}