#K85272. Maximum Subarray Sum of Fixed Length

    ID: 36605 Type: Default 1000ms 256MiB

Maximum Subarray Sum of Fixed Length

Maximum Subarray Sum of Fixed Length

You are given an integer n denoting the number of elements in an array, and an integer k denoting the fixed length of a contiguous subarray. Your task is to find the maximum sum of any contiguous subarray of the array that is exactly of length k.

If k is greater than n or if the list is empty, the answer should be 0.

The solution should be efficient and use a sliding window technique. The mathematical formulation of the problem is as follows:

Given an array \( A = [a_1, a_2, \dots, a_n] \) and a positive integer \( k \), find \[ \max_{1 \leq i \leq n-k+1} \sum_{j=i}^{i+k-1} a_j \]

inputFormat

The first line contains two integers \( n \) and \( k \) separated by space, where \( n \) is the number of elements in the array and \( k \) is the fixed subarray length.

The second line contains \( n \) integers separated by spaces, representing the elements of the array.

outputFormat

Output a single integer representing the maximum sum of any contiguous subarray of length \( k \). If \( k \) is greater than \( n \) or the array is empty, output 0.

## sample
10 3
1 2 3 4 5 6 7 8 9 10
27