#K59937. Maximum Sum Subarray of Fixed Size
Maximum Sum Subarray of Fixed Size
Maximum Sum Subarray of Fixed Size
Given an array of n integers and a positive integer m, find the contiguous subarray of size m that has the maximum sum. This problem can be efficiently solved using a sliding window algorithm.
Mathematically, if the array is \(a_1, a_2, \ldots, a_n\), you need to compute:
\[ S = \max_{1 \leq k \leq n-m+1}\left( \sum_{i=k}^{k+m-1} a_i \right) \]
Note that \(m\) is guaranteed to be less than or equal to \(n\). The input is read from stdin and the output is printed to stdout.
inputFormat
The first line contains two space-separated integers: (n) (the number of elements) and (m) (the size of the subarray). The second line contains (n) space-separated integers representing the array elements.
outputFormat
Output a single integer representing the maximum sum of any contiguous subarray of size (m).## sample
10 3
1 2 3 4 5 6 7 8 9 10
27