#K6791. Maximum Sum of a Subarray of Fixed Size
Maximum Sum of a Subarray of Fixed Size
Maximum Sum of a Subarray of Fixed Size
You are given an array of integers and an integer k. Your task is to find the maximum sum of any contiguous subarray of size k.
Formally, for an array \(A = [a_1, a_2, \dots, a_n]\) and an integer \(k\), you need to compute:
\(\max_{1 \leq i \leq n-k+1} \sum_{j=i}^{i+k-1} a_j\)
If the array length is less than k, then the answer is defined to be 0.
This problem can be solved efficiently using the sliding window technique.
inputFormat
The first line contains two integers n and k, where n is the number of elements in the array, and k is the size of the subarray.
The second line contains n integers separated by spaces representing the elements of the array.
outputFormat
Output a single integer representing the maximum sum of any contiguous subarray of size k. If no such subarray exists (i.e. if n < k), output 0.
## sample5 2
1 2 3 4 5
9