Maximum Subarray Sum Across K Concatenations
Problem statement
Form a conceptual array by concatenating k copies of the nonempty integer array values. Return the maximum sum of a nonempty contiguous subarray of that conceptual array.
Return the exact signed 64-bit value; do not apply a modulus.
Function
maxKConcatenationSum(values: int[], k: int) → longExamples
Example 1
values = [1,-2,1]k = 5return = 2The best subarray crosses a copy boundary and contains the last and first 1.
Example 2
values = [-4,-2,-7]k = 3return = -2The subarray must be nonempty, so the largest single value is returned.
Constraints
1 <= values.length <= 2000001 <= k <= 10^9-10^9 <= values[i] <= 10^9- The answer fits in a signed 64-bit integer.