#K81007. Maximum Sum of Contiguous Subarray of Length k
Maximum Sum of Contiguous Subarray of Length k
Maximum Sum of Contiguous Subarray of Length k
You are given an array of integers representing bytes streamed over time, and an integer k. Your task is to compute the maximum sum of any contiguous subarray of length k. If the array length is less than k, the answer is 0.
In mathematical terms, given an array \(a_1, a_2, \dots, a_n\) and an integer \(k\), you need to determine \[ \max_{1 \leq i \leq n-k+1} \sum_{j=i}^{i+k-1} a_j, \] with the understanding that if \(n < k\), then the result is 0.
This problem can be efficiently solved using a sliding window technique with a time complexity of \(O(n)\).
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains two space-separated integers, n (the number of elements in the array) and k (the length of the subarray).
- If n > 0, the second line contains n space-separated integers representing the elements of the array. If n = 0, the second line will be empty.
outputFormat
Output a single integer to stdout, representing the maximum sum of any contiguous subarray of length k. If no such subarray exists (i.e. when n < k), output 0.
## sample7 3
1 3 2 6 2 3 5
11