NvidiaOA #2
Maximum Subarray of Size at Least K
Estimated MediumJudge-ready
Topics (AI-labelled)Prefix & RangesDP Basics
Choose a non-empty contiguous subarray of length at least K that maximizes the sum of its elements. The catalog's second example answer of −5 is wrong; [-2,1] has the larger sum −1.
This site uses a standard input/output protocol.
Input
The first line contains N K, and the second line contains N integers. This site uses 1≤K≤N≤100000 and elements in −10⁹..10⁹.
Output
Output a single integer, the answer.
Example 1
Input:
3 2
10 -2 5
Output:
13
Example 2
Input:
5 2
-3 -2 1 -6 -30
Output:
-1
Example 3
Input:
3 1
-8 -2 -7
Output:
-2
Example notes
Example 1: choose three elements, for a total of 13. Example 2: choose −2, 1, for a total of −1, correcting the source example's −5. Example 3: choose the single element −2; an empty array is not allowed.
Editorial
Loading solution…