#K15016. Maximum Sum Segments
Maximum Sum Segments
Maximum Sum Segments
You are given an array of n integers and an integer w representing the length of a segment. Your task is to determine the maximum sum of any segment consisting of w consecutive elements and count the number of segments that achieve this sum.
Formally, let \(a_1,a_2,\dots,a_n\) be the array and define the segment sum \(S_i = \sum_{j=i}^{i+w-1}a_j\) for \(1 \le i \le n-w+1\). You need to find \(M = \max_{1\le i \le n-w+1}{S_i}\) and the number of indices \(i\) such that \(S_i = M\).
If w is 0 or if the array is empty, output 0 0
.
inputFormat
The input consists of two lines:
- The first line contains two space-separated integers:
n
(the number of elements in the array) andw
(the length of the segment). - The second line contains
n
space-separated integers representing the array elements.
You can assume that the input is valid and n
is at least equal to w
when w > 0
.
outputFormat
Output a single line containing two space-separated integers: the maximum sum of any segment of length w
and the number of segments that achieve this maximum sum.
10 3
1 2 3 4 5 6 7 8 9 10
27 1
</p>