Maximum Sum Subarray of Size K (Easy)
Maximum Sum Subarray of Size K (Easy)
• Problem Statement
• Try it yourself
• Solution
• Code
• A better approach
• Time Complexity
• Space Complexity
Problem Statement
Given an array of positive numbers and a positive number ‘k,’ find the maximum sum of any contiguous subarray of size ‘k’.
Example 1:
Try it yourself
Try solving this question here:
class MaxSumSubArrayOfSizeK {
public static int findMaxSumSubArray(int k, int[] arr) {
// TODO: Write your code here
return -1;
}
}
Solution
A basic brute force solution will be to calculate the sum of all ‘k’ sized subarrays of the given array to find the subarray with the
highest sum. We can start from every index of the given array and add the next ‘k’ elements to find the subarray’s sum. Following
is the visual representation of this algorithm for Example-1:
K=3
Window Sum = 0
2 1 5 1 3 2
Max Sum = 0
Window Sum = 8 2 1 5 1 3 2
Max Sum = 8
Window Sum = 7
2 1 5 1 3 2
Max Sum = 8
Window Sum = 9
Max Sum = 9 2 1 5 1 3 2
Window Sum = 6
2 1 5 1 3 2
Max Sum = 9
Code
Here is what our algorithm will look like:
class MaxSumSubArrayOfSizeK {
public static int findMaxSumSubArray(int k, int[] arr) {
int maxSum = 0, windowSum;
for (int i = 0; i <= arr.length - k; i++) {
windowSum = 0;
for (int j = i; j < i + k; j++) {
windowSum += arr[j];
}
maxSum = Math.max(maxSum, windowSum);
}
return maxSum;
}
The above algorithm’s time complexity will be O(N ∗ K), where ‘N’ is the total number of elements in the given array. Is it
possible to find a better algorithm than this?
A better approach
If you observe closely, you will realize that to calculate the sum of a contiguous subarray, we can utilize the sum of the previous
subarray. For this, consider each subarray as a Sliding Window of size ‘k.’ To calculate the sum of the next subarray, we need to
slide the window ahead by one element. So to slide the window forward and calculate the sum of the new position of the sliding
window, we need to do two things:
1. Subtract the element going out of the sliding window, i.e., subtract the first element of the window.
2. Add the new element getting included in the sliding window, i.e., the element coming right after the end of the window.
This approach will save us from re-calculating the sum of the overlapping part of the sliding window. Here is what our algorithm
will look like:
class MaxSumSubArrayOfSizeK {
public static int findMaxSumSubArray(int k, int[] arr) {
int windowSum = 0, maxSum = 0;
int windowStart = 0;
for (int windowEnd = 0; windowEnd < arr.length; windowEnd++) {
windowSum += arr[windowEnd]; // add the next element
// slide the window, we don't need to slide if we've not hit the required window size of 'k'
if (windowEnd >= k - 1) {
maxSum = Math.max(maxSum, windowSum);
windowSum -= arr[windowStart]; // subtract the element going out
windowStart++; // slide the window ahead
}
}
return maxSum;
}
Time Complexity
The time complexity of the above algorithm will be O(N ).
Space Complexity
The algorithm runs in constant space O(1).