#K33212. Maximum Sum of K Consecutive Elements
Maximum Sum of K Consecutive Elements
Maximum Sum of K Consecutive Elements
You are given an array of integers and an integer k. Your task is to compute the maximum sum of any k consecutive elements in the array. If the array contains fewer than k elements, output 0.
This problem can be formulated mathematically as finding $$\max_{0 \leq i \leq n-k}\;\sum_{j=i}^{i+k-1} a_j,$$ where \(n\) is the total number of elements. A sliding window approach provides an efficient solution with linear time complexity.
inputFormat
The input is read from stdin and consists of the following parts:
- An integer n representing the number of elements in the array.
- n space-separated integers representing the array elements.
- An integer k representing the number of consecutive elements to consider.
outputFormat
Output a single integer to stdout representing the maximum sum of any k consecutive elements in the array. If the array has fewer than k elements, output 0.
## sample5
1 2 3 4 5
2
9
</p>