#K3936. Maximum Sum Subarray of Fixed Size

    ID: 26403 Type: Default 1000ms 256MiB

Maximum Sum Subarray of Fixed Size

Maximum Sum Subarray of Fixed Size

Given an array of integers and an integer (k), your task is to find the maximum sum of any contiguous subarray of exactly (k) elements. If the array contains fewer than (k) elements or if (k \le 0), then the output should be None.

Input: The input is read from standard input. The first number is an integer (T) representing the number of test cases. Each test case begins with two integers: (n) (the size of the array) and (k) (the number of elements in the subarray). The next (n) integers denote the elements of the array.

Output: For each test case, print the maximum sum of any contiguous subarray of size (k) on a new line. If no such subarray exists, print None.

Example: Consider the test case where (n = 5), (k = 2), and the array is [1, 2, 3, 4, 5]. The possible contiguous subarrays of size 2 are [1,2], [2,3], [3,4], [4,5] and their sums are 3, 5, 7, and 9 respectively. Hence, the output is 9.

inputFormat

The input is given via standard input (stdin). The first line contains an integer (T), the number of test cases. Each test case consists of two lines:

  • The first line contains two space-separated integers (n) and (k).
  • The second line contains (n) space-separated integers representing the elements of the array.

outputFormat

For each test case, output the maximum sum of any contiguous subarray of exactly (k) elements. If the subarray of size (k) does not exist (i.e. if (n < k) or (k \le 0)), output None. Each answer should be printed on a new line.## sample

5
5 2
1 2 3 4 5
5 5
1 2 3 4 5
2 3
1 2
5 1
5 4 3 2 1
5 3
4 -1 2 1 6
9

15 None 5 9

</p>