#K74672. Maximum Sum Subarray of Fixed Length
Maximum Sum Subarray of Fixed Length
Maximum Sum Subarray of Fixed Length
Given an array of n integers and a positive integer k, your task is to find a subarray (a continuous segment) of length exactly k that has the maximum sum. Formally, if the array is represented as \( A_1, A_2, \ldots, A_n \), you need to find an index \( j \) (where \(1 \le j \le n-k+1\)) such that the sum
\(S = \sum_{i=j}^{j+k-1} A_i\)
is maximized. In addition to the maximum sum, output the 1-indexed positions of the chosen subarray elements.
Note: If multiple subarrays yield the same maximum sum, you may output the indices of the first one encountered.
inputFormat
The first line contains two integers n and k separated by a space.
The second line contains n integers separated by spaces, representing the array elements.
outputFormat
Output two lines:
- The first line should contain the maximum sum of a subarray of length k.
- The second line should contain k integers separated by spaces, which are the 1-indexed positions of the elements in that subarray.
6 3
4 -1 2 1 -5 4
5
1 2 3
</p>