Max Sum Subarray

Sliding Window Newyear Coding Challenge

Max Sum Subarray

In the spirit of New Year's celebrations, imagine you have an array where each element represents the number of festivities or celebrations on consecutive days. Your task is to find the maximum total number of celebrations that can be attended over any period of k consecutive days.

Problem Statement:

Given an array of integers arr and an integer k, write a function to calculate the maximum sum of any contiguous subarray of length k.

The expected time complexity is O(n), using a sliding window technique.

Example:

For the array arr = [2, 1, 5, 1, 3, 2] and k = 3, the contiguous subarrays of length 3 are [2, 1, 5], [1, 5, 1], [5, 1, 3], and [1, 3, 2]. The maximum sum among these is 5+1+3 = 9.

Function Signature:

Implement the following function:

// Input: arr (array of integers), k (integer: window size)
// Output: integer (maximum sum of k consecutive elements in arr)
function maxSumSubarray(arr, k) {}

Happy coding and Happy New Year!