#K49022. Maximum Sum of Subarray of Length K
Maximum Sum of Subarray of Length K
Maximum Sum of Subarray of Length K
Given an array \(A\) of \(n\) integers and an integer \(k\), your task is to find the maximum sum of any contiguous subarray of exactly length \(k\). In other words, you need to compute
\(\max_{0 \leq i \leq n-k}\left(\sum_{j=i}^{i+k-1} A_j\right)\)
and output this maximum sum.
The most efficient solution uses a sliding window approach, which runs in \(O(n)\) time.
inputFormat
The input is given from standard input and includes:
- The first line contains two 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 integers representing the elements of the array \(A\).
outputFormat
Print a single integer representing the maximum sum of any contiguous subarray of length exactly \(k\) to standard output.
## sample5 3
1 2 3 4 5
12