Given an array of integers and a positive integer k
, write a function that returns the maximum sum of any contiguous subarray of length k
.
If the length of the array is less than k
, the function should return null
(or an equivalent value based on your language) since no valid subarray exists.
For example, given the array [2, 3, 4, 1, 5]
and k = 3
, the contiguous subarrays of length 3 are:
[2, 3, 4]
with a sum of 9
[3, 4, 1]
with a sum of 8
[4, 1, 5]
with a sum of 10
The function should therefore return 10
.
On April 4th, many cultures celebrate renewal and new beginnings. This problem is a great way to refresh your algorithm skills by exploring a common sliding window technique.
Below is some starter code for various programming languages to help you begin your solution.