#C7716. Maximum Sum Subarray of Fixed Length
Maximum Sum Subarray of Fixed Length
Maximum Sum Subarray of Fixed Length
You are given an array (arr) of positive integers 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 a sliding window technique. The sliding window method works by first computing the sum of the initial window of size (k) and then sliding the window one element at a time while updating the sum. Mathematically, if the current window sum is (S) and the next element is (a_{i}) while the element leaving the window is (a_{i-k}), the new sum is given by:
[
S_{new} = S + a_{i} - a_{i-k}
]
Your solution should read the input from standard input and output the result to standard output.
inputFormat
The input consists of two lines:
- The first line contains two integers (n) and (k) separated by a space, where (n) is the number of elements in the array and (k) is the length of the subarray.
- The second line contains (n) positive integers separated by spaces which represent the elements of the array.
outputFormat
Output a single integer which is the maximum sum of any contiguous subarray of length (k).## sample
6 3
2 1 5 1 3 2
9