#K75057. Maximum Sum of K Consecutive Elements
Maximum Sum of K Consecutive Elements
Maximum Sum of K Consecutive Elements
Given an array of n integers, your task is to find the maximum sum of any subarray containing exactly k consecutive elements. Formally, if the array is \(a = [a_0, a_1, \dots, a_{n-1}]\), you need to compute:
\(\max_{0 \le i \le n-k} \sum_{j=i}^{i+k-1} a_j\)
This problem can be solved efficiently using a sliding window technique. Note that the array may contain negative numbers, so make sure to handle all edge cases.
inputFormat
The first line of input contains two space-separated integers: n
(the number of elements in the array) and k
(the number of consecutive elements to consider). The second line contains n
space-separated integers representing the elements of the array.
outputFormat
Output a single integer representing the maximum sum of any subarray of exactly k
consecutive elements.
5 2
1 2 3 4 5
9
</p>