"""
Given an array of positive numbers and a positive number ‘k’, find the maximum
sum of any contiguous subarray of size ‘k’.

Example:
    Input: [2, 1, 5, 1, 3, 2], k=3
    Output: 9
    Explanation: Subarray with maximum sum is [5, 1, 3].

Time Complexity: O(n)
Space Complexity: O(1)
"""


def max_subarray_of_size_k(arr: list, k: int):
    window_start = window_sum = max_window_sum = 0
    for window_end in range(len(arr)):
        window_sum += arr[window_end]
        if window_end >= k - 1:
            print(f"sum: {window_sum}")
            # max_window_sum = window_sum if window_sum >= max_window_sum else max_window_sum
            max_window_sum = max(window_sum, max_window_sum)  # more pythonic
            window_sum -= arr[window_start]
            window_start += 1

    return max_window_sum


if __name__ == "__main__":
    arr = [2, 1, 5, 1, 3, 2]
    k = 3
    print(f"original array: {arr}")
    max_subarray_sum = max_subarray_of_size_k(arr, k)
    print(f"max subarray sum = {max_subarray_sum}")
