#K90342. Maximum Sliding Window Sum

    ID: 37731 Type: Default 1000ms 256MiB

Maximum Sliding Window Sum

Maximum Sliding Window Sum

Given an array of n integers and a sliding window of size k, your task is to compute the maximum sum of any contiguous subarray (window) of length k.

The sum for a window starting at index i is defined as:

\(S_i = a_{i} + a_{i+1} + \cdots + a_{i+k-1}\)

Your objective is to find \(\max\{S_i\}\) for all valid \(i\). If k is greater than n or if k = 0, then output 0.

Example: For n = 8, k = 3 and array = [1, 3, -1, -3, 5, 3, 6, 7], the maximum sliding window sum is 16.

inputFormat

The input is given in two lines:

  • The first line contains two integers n and k, where n is the number of elements in the array, and k is the size of the sliding window.
  • The second line contains n space-separated integers representing the elements of the array.

outputFormat

Output a single integer — the maximum sum of any contiguous subarray (window) of size k.

## sample
8 3
1 3 -1 -3 5 3 6 7
16