#C2242. 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 integers and a positive integer w. Your task is to find the maximum sum of any contiguous subarray of length exactly w.
In other words, if the array is denoted by \(a_1, a_2, \dots, a_n\), you need to determine the maximum possible value of \[ S = a_i + a_{i+1} + \cdots + a_{i+w-1} \] for any valid index \(i\) such that \(1 \le i \le n-w+1\).
If w
is greater than n
, your program should output the error message "Window size w must be less than or equal to the length of the list".
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains two integers
n
andw
, wheren
is the number of elements in the array, andw
is the fixed subarray length. - The second line contains
n
integers separated by spaces representing the elements of the array.
You may assume that all integers are within a reasonable range to avoid overflow issues.
outputFormat
If w
is less than or equal to n
, print a single integer on stdout---the maximum sum of any contiguous subarray of length w
.
If w > n
, print the following error message exactly as shown:
Window size w must be less than or equal to the length of the list## sample
6 2
1 2 3 4 5 6
11
</p>