Maximum Subarray Sum With Length Divisible by K

You are given an array of integers nums and an integer k.

Return the maximum sum of a subarray of nums, such that the size of the subarray is divisible by k.

Example 1
Inputnums = [1,2], k = 1
Output3
The subarray [1, 2] with sum 3 has length equal to 2 which is divisible by 1.
Example 2
Inputnums = [-1,-2,-3,-4,-5], k = 4
Output-10
The maximum sum subarray is [-1, -2, -3, -4] which has length equal to 4 which is divisible by 4.

Constraints

  • 1 <= k <= nums.length <= 2 * 10^5
  • -10^9 <= nums[i] <= 10^9

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate