#C7090. 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 an integer M representing the size of a subarray. Your task is to find the maximum sum of any contiguous subarray of length exactly M.
The problem can be formally stated as follows: Given an array \(A\) of length \(N\) and an integer \(M\), find the maximum value of the sum: \[ S = \sum_{i=k}^{k+M-1} A_i \] for all valid indices \(k\) such that \(0 \leq k \leq N-M\).
Input Format: The first line contains two integers \(N\) and \(M\). The second line contains \(N\) space-separated integers representing the array \(A\).
Output Format: Print a single integer which is the maximum sum of any contiguous subarray of length \(M\).
Example:
Input: 5 3 2 1 5 1 3</p>Output: 9
inputFormat
The first line contains two integers N and M separated by a space. The second line contains N integers separated by spaces representing the array A.
outputFormat
A single integer which is the maximum sum of any contiguous subarray of length M.## sample
5 3
2 1 5 1 3
9