Write a function called maxSubarraySum
that takes an array of integers and a positive integer k
as input. The function should find the maximum sum of any subarray of length k
using an O(n^2) solution by using nested for loops.
We will re-visit this and use the sliding window technique to use an O(n) solution.
/**
* Finds the maximum sum of any subarray of length k in the input array using an O(n^2) solution.
* @param {number[]} arr - The input array of integers.
* @param {number} k - The length of the subarray.
* @returns {number} - The maximum sum of any subarray of length k.
*/
function maxSubarraySum(arr: number[], k: number): number
const arr1 = [2, 5, 3, 1, 11, 7, 6, 4];
const k1 = 3;
console.log(maxSubarraySum(arr1, k1)); // Output: 24
const arr2 = [-2, -5, -3, -1, -11, -7, -6, -4];
const k2 = 4;
console.log(maxSubarraySum(arr2, k2)); // Output: -9
- The input integer
k
will be between 1 and the length of the array.
- You can use two nested loops to iterate through all possible subarrays of length
k
and calculate their sums.
Click For Solution
function maxSubarraySum(arr, k) {
let maxSum = 0;
for (let i = 0; i <= arr.length - k; i++) {
let currentSum = 0;
for (let j = i; j < i + k; j++) {
currentSum += arr[j];
}
maxSum = Math.max(maxSum, currentSum);
}
return maxSum;
}
- The function
maxSubarraySum
uses two nested loops to iterate through all possible subarrays of lengthk
. - For each subarray, it calculates the sum using a nested loop and keeps track of the maximum sum encountered.
- Finally, it returns the maximum sum.
test('Finding maximum subarray sum using O(n^2) solution', () => {
const arr1 = [2, 5, 3, 1, 11, 7, 6, 4];
const k1 = 3;
expect(maxSubarraySum(arr1, k1)).toBe(24);
const arr2 = [-2, -5, -3, -1, -11, -7, -6, -4];
const k2 = 4;
expect(maxSubarraySum(arr2, k2)).toBe(-9);
});
Please note that the provided solution has a time complexity of O(n^2) due to the nested loops.