Create a function that finds the maximum sum of any subarray of fixed size k in a given array of integers. Return -1 if k is invalid (k <= 0 or k > length of the array).
Examples:
max_subarray_sum_of_size_k([1,4,2,10,23,3,1,0,20], 4)returns29(subarray [4,2,10,23)max_subarray_sum_of_size_k([-2, -3, -1, -4], 2)returns-3
def max_subarray_sum_of_size_k(arr, k):
"""One-line docstring placeholder"""
pass
Rules:
- Any language or pseudocode is fine. The stub just shows the shape.
- Say the time complexity of your approach.
- short rule
- O(n) time, O(1) extra space
- works for negative numbers
- edge case handling for invalid k
Post your solution as a reply. Answer goes up in about a day.