#C9746. Maximum Sum Subarray of Fixed Length
Maximum Sum Subarray of Fixed Length
Maximum Sum Subarray of Fixed Length
You are given an array of integers and an integer k. Your task is to find the maximum sum of any contiguous subarray of length exactly k.
More formally, given an array \(a_1, a_2, \dots, a_n\) and an integer \(k\), you need to compute:
[ \text{max_sum} = \max_{1 \le i \le n-k+1} \left(\sum_{j=i}^{i+k-1} a_j\right) ]
If the array is empty or if \(k > n\), output 0.
inputFormat
The input is given via standard input (stdin) and consists of two lines:
- The first line contains two space-separated integers: n (the number of elements in the array) and k (the length of the subarray).
- The second line contains n space-separated integers representing the elements of the array. When n is 0, the second line will be empty.
outputFormat
Output a single integer via standard output (stdout) which is the maximum sum of any contiguous subarray of length k. If no such subarray exists, output 0.
## sample5 2
1 2 3 4 5
9
</p>