#K74962. Maximum Sum Subarray of Fixed Size

    ID: 34314 Type: Default 1000ms 256MiB

Maximum Sum Subarray of Fixed Size

Maximum Sum Subarray of Fixed Size

You are given an array of integers and a positive integer \(k\). Your task is to find the contiguous subarray of length \(k\) that has the maximum sum among all such subarrays. In case of a tie (multiple subarrays with the same maximum sum), return the first one.

Example:

  • For the array [1, 2, 5, 2, 8, 1, 5] and \(k = 2\), the subarray with the maximum sum is [2, 8].
  • For the array [1, -2, 3, -4, 5, -6, 7] and \(k = 3\), the subarray with the maximum sum is [5, -6, 7].

Note: The input is read from standard input and the result should be printed to standard output. The subarray elements in the output should be space-separated.

inputFormat

The first line of input contains two integers \(n\) and \(k\) where \(n\) is the number of elements in the array and \(k\) is the length of the subarray to consider. The second line contains \(n\) space-separated integers representing the array elements.

Example:

7 2
1 2 5 2 8 1 5

outputFormat

Output a single line containing \(k\) space-separated integers representing the contiguous subarray which has the maximum sum.

Example:

2 8
## sample
7 2
1 2 5 2 8 1 5
2 8