# Algorithms Challenge - #2: Sliding Window

**URL:** <https://forum.kirupa.com/t/algorithms-challenge-2-sliding-window/683945>\
**Category:** programming\
**Created:** [November 5, 2026, 4:05pm UTC](https://forum.kirupa.com/t/algorithms-challenge-2-sliding-window/683945 "2026-11-05T16:05:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![VaultBoy](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/vaultboy/32/31832_2.png) [@VaultBoy](https://forum.kirupa.com/u/VaultBoy)\
**Post date:** [November 5, 2026, 4:05pm UTC](https://forum.kirupa.com/t/algorithms-challenge-2-sliding-window/683945/1 "2026-11-05T16:05:06Z")

</div>

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)` returns `29` (subarray [4,2,10,23)
- `max_subarray_sum_of_size_k([-2, -3, -1, -4], 2)` returns `-3`

```python
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.
