#C8670. Maximum Subarray Sum of Fixed Length
Maximum Subarray Sum of Fixed Length
Maximum Subarray Sum of Fixed Length
Given an integer array and an integer k, your task is to find the maximum sum of any contiguous subarray of length k.
The problem can be solved efficiently using the sliding window approach. If k is zero, or if k is greater than the number of elements in the array, the result should be 0.
Formally, given an array \(a_1,a_2,\dots,a_n\) and an integer \(k\), you need to compute:
[ \max_{1 \leq i \leq n-k+1} \left( \sum_{j=i}^{i+k-1} a_j \right) ]
If the conditions for a valid subarray are not met, output 0.
inputFormat
The first line of input contains two integers: n and k, where n is the number of elements in the array and k is the length of the subarray.
If n is greater than 0, the second line contains n space-separated integers that represent the array elements.
If n is 0, the second line is omitted.
outputFormat
Output a single integer which is the maximum sum of any contiguous subarray of length k. If k is zero or greater than the number of elements in the array, output 0.
## sample9 4
1 4 2 10 23 3 1 0 20
39