Back to problems

Maximum Subarray Sum with Length at Most K

Algorithm · Oracle · Hard

Given an array nums of length n and an integer K, choose a non-empty contiguous block nums[i], nums[i+1], ..., nums[j] such that $$0 \le i \le j < n$$ and $$j - i + 1 \le K$$. Find the largest possible sum over all such blocks. The input is given on two lines: the first line contains n and K, and the second line contains the n integers of nums. Print a single integer: the maximum sum. Example 1: Explanation: The block [3, 4] has length 2, which is at most K = 3, and its sum…

Checking your access…