#K356. Maximum Subarray Sum for Fixed-Length Windows

    ID: 25567 Type: Default 1000ms 256MiB

Maximum Subarray Sum for Fixed-Length Windows

Maximum Subarray Sum for Fixed-Length Windows

You are given an integer \(T\) representing the number of test cases. For each test case, you are given two integers \(n\) and \(k\) on the first line, followed by \(n\) integers on the second line. Your task is to find the maximum possible sum of any contiguous subarray of exactly \(k\) elements.

Note: A subarray is a contiguous sequence of elements within an array. You must use a sliding window approach for an efficient solution.

inputFormat

The input is read from stdin and has the following format:

T
n k
a1 a2 ... an
... (repeated for T test cases)

Where:

  • T is the number of test cases.
  • For each test case, the first line contains two integers \(n\) (the size of the array) and \(k\) (the length of the subarray).
  • The second line contains \(n\) space-separated integers.

outputFormat

For each test case, output the maximum possible sum of any subarray of length \(k\> on a new line. The output is printed to stdout.

## sample
1
5 2
1 2 3 4 5
9

</p>