FastPrepMaximum Subarray Sum Across K Concatenations

Maximum Subarray Sum Across K Concatenations

Microsoft logoMicrosoft● MediumFULLTIMEONSITE INTERVIEW
Learn

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) → long

Examples

Example 1

values = [1,-2,1]k = 5return = 2

The best subarray crosses a copy boundary and contains the last and first 1.

Example 2

values = [-4,-2,-7]k = 3return = -2

The subarray must be nonempty, so the largest single value is returned.

Constraints

  • 1 <= values.length <= 200000
  • 1 <= k <= 10^9
  • -10^9 <= values[i] <= 10^9
  • The answer fits in a signed 64-bit integer.

More Microsoft problems

See Microsoft hiring insights
public long maxKConcatenationSum(int[] values, int k) {
    // Write your code here.
}
values[1,-2,1]
k5
expected2
Checking account…