#C11116. Maximum Sum of k Consecutive Elements
Maximum Sum of k Consecutive Elements
Maximum Sum of k Consecutive Elements
Given an array of integers and an integer k, your task is to find the maximum sum of any k consecutive elements in the array. More formally, if the array is defined as \(a_0, a_1, \dots, a_{n-1}\), you need to compute
\(S = \max_{0 \leq i \leq n-k}\,\sum_{j=i}^{i+k-1}a_j\)
This is a classic sliding window problem. Make sure that your solution is efficient enough to handle large inputs.
inputFormat
The first line of input contains two integers n and k separated by a space, where n is the number of elements in the array and k is the number of consecutive elements to consider. The second line contains n space-separated integers representing the array elements.
It is guaranteed that 1 ≤ k ≤ n
and that the array contains at least k
elements.
outputFormat
Output a single integer, which is the maximum sum of any k consecutive elements in the array.
## sample5 2
1 2 3 4 5
9