#C6249. Maximum Subarray Sum of Fixed Length
Maximum Subarray Sum of Fixed Length
Maximum Subarray Sum of Fixed Length
You are given an array of integers and a positive integer \(L\). Your task is to find the maximum sum of any contiguous subarray of the array that has exactly length \(L\). If there is no such subarray because \(L\) is greater than the length of the array, output \(-1\).
Details:
- Let \(A = [a_1, a_2, \dots, a_n]\) be the given array.
- You need to compute \(\max_{1 \leq i \leq n-L+1} \sum_{j=i}^{i+L-1} a_j\), or report \(-1\) if \(L > n\).
Example:
Input: 5 3 1 2 3 4 5</p>Output: 12
inputFormat
The input is read from stdin and has the following format:
T N1 L1 A1,1 A1,2 ... A1,N1 N2 L2 A2,1 A2,2 ... A2,N2 ... NT LT AT,1 AT,2 ... AT,NT
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 \(L\) (the required subarray length).
- The next line contains \(N\) space-separated integers representing the elements of the array.
outputFormat
For each test case, output a single integer representing the maximum sum of any subarray of length \(L\) on a separate line. If no such subarray exists, output \(-1\).
The output should be written to stdout.
## sample1
5 3
1 2 3 4 5
12
</p>