#K6511. Maximum Sum Subarray of Size K

    ID: 32125 Type: Default 1000ms 256MiB

Maximum Sum Subarray of Size K

Maximum Sum Subarray of Size K

You are given an array of integers and a positive integer k. Your task is to compute the maximum sum of any contiguous subarray of length k.

Consider an array \(a_1, a_2, \dots, a_n\). For a fixed integer \(k\) (\(1 \leq k \leq n\)), you need to find the maximum value of

\(S = \sum_{i=j}^{j+k-1} a_i\)

where \(1 \leq j \leq n-k+1\). A sliding window technique can be used to achieve an efficient solution with a time complexity of \(O(n)\). Note that when \(k = n\), the answer is simply the sum of the entire array.

inputFormat

Input is given via standard input (stdin) and is structured as follows:

  • The first line contains two integers \(n\) and \(k\), where \(n\) is the number of elements in the array and \(k\) is the subarray length.
  • The second line contains \(n\) space-separated integers which represent the elements of the array.

outputFormat

Output via standard output (stdout) a single integer representing the maximum sum of any contiguous subarray of size \(k\).

## sample
6 3
2 1 5 1 3 2
9

</p>