#K11171. Maximum Sum Subarray of Fixed Length
Maximum Sum Subarray of Fixed Length
Maximum Sum Subarray of Fixed Length
You are given an array of n non-negative integers, and a positive integer k. Your task is to find a contiguous subarray of length exactly k that has the maximum sum among all possible subarrays of this length.
In mathematical terms, if the array is represented as \(A = [a_0, a_1, \dots, a_{n-1}]\), you need to compute:
\(\max_{0 \leq i \leq n-k} \sum_{j=i}^{i+k-1} a_j\)
If n or k is zero, the result should be 0.
inputFormat
The input is provided via stdin and consists of:
- The first line contains two space-separated integers n and k — the number of elements in the array and the length of the subarray respectively.
- The second line contains n space-separated non-negative integers representing the array.
outputFormat
The output should be written to stdout and consists of a single integer — the maximum sum of any contiguous subarray of length k. If n or k is zero, output 0.
## sample6 3
1 2 3 4 5 6
15