#K72112. Fixed-Length Subarray Maximum Sum
Fixed-Length Subarray Maximum Sum
Fixed-Length Subarray Maximum Sum
You are given an array of n integers and a positive integer k. Your task is to compute the maximum sum of any contiguous subarray of length k. Specifically, if the array is denoted as \(a_1, a_2, \dots, a_n\), you need to find \(\max_{1 \leq i \leq n-k+1} \sum_{j=i}^{i+k-1} a_j\).
If \(n < k\), output 0. This problem requires a sliding window approach to efficiently compute subarray sums.
inputFormat
The first line contains two integers n
and k
, where n
is the number of elements in the array and k
is the length of the contiguous subarray to consider. The second line contains n
space-separated integers representing the array.
Input Format:
n k a1 a2 a3 ... an
outputFormat
Output a single integer — the maximum sum of any contiguous subarray with length k
. If n < k
, output 0.
Output Format:
max_sum## sample
6 3
2 1 5 1 3 2
9