k
and store it in array W
.left
, right
, and sum
arrays of the same length as W
.left
array with the starting index of the maximum subarray sum ending at or before each index.right
array with the starting index of the maximum subarray sum starting at or after each index.W
using j
as the middle subarray's starting index, and for each j
, calculate the sum of subarrays starting at left[j - k]
, j
, and right[j + k]
.