Maximum Sum Subarray of Size K

EasySliding Window
Next
ArraySliding Window

Find the largest sum of any contiguous subarray that has exactly k elements.

Input

Line 1: n and k. Line 2: n integers.

Output

Print the maximum sum of a subarray of length k.

Constraints

1 <= k <= n <= 10^5 -10^4 <= each value <= 10^4

Example 1
Input
6 3
2 1 5 1 3 2
Output
9

The window 5 1 3 sums to 9, the largest of any three in a row.

Hints

Maximum Sum Subarray of Size K - Easy Sliding Window Problem | Skill Shiksha