Formatted question description: https://leetcode.ca/all/1191.html

1191. K-Concatenation Maximum Sum

Level

Medium

Description

Given an integer array arr and an integer k, modify the array by repeating it k times.

For example, if arr = [1, 2] and k = 3 then the modified array will be [1, 2, 1, 2, 1, 2].

Return the maximum sub-array sum in the modified array. Note that the length of the sub-array can be 0 and its sum in that case is 0.

As the answer can be very large, return the answer modulo 10^9 + 7.

Example 1:

Input: arr = [1,2], k = 3

Output: 9

Example 2:

Input: arr = [1,-2,1], k = 5

Output: 2

Example 3:

Input: arr = [-1,-2], k = 7

Output: 0

Constraints:

  • 1 <= arr.length <= 10^5
  • 1 <= k <= 10^5
  • -10^4 <= arr[i] <= 10^4

Solution

First calculate the maximum subarray sum in arr without repeating. This can be achieved using the solution to problem 53. If k == 1, simply return the maximum subarray sum.

Then calculate the sum of all the elements of arr, the maximum prefix sum of arr and the maximum postfix sum of arr. All the sums are calculated in the single arr without repeating. Update the maximum sum by the maximum subarray sum and the sum of the maximum postfix sum and the maximum prefix sum. If the sum of all the elements of arr is greater than 0, then update the maximum sum using the following information: the sum of all the elements of arr multiplied by k, the maximum postfix sum plus the sum of all the elements of arr multiplied by k - 2 plus the maximum prefix sum, the maximum postfix sum plus the sum of all the elements of arr multiplied by k - 1, and the sum of all the elements of arr multiplied by k - 1 plus the maximum prefix sum. Finally, rethrn the maximum sum.

class Solution {
    public int kConcatenationMaxSum(int[] arr, int k) {
        final int MODULO = 1000000007;
        long maxSubArraySum = maxSubArraySum(arr);
        if (k == 1)
            return (int) (maxSubArraySum % MODULO);
        long sum = 0;
        long maxPrefixSum = 0;
        int length = arr.length;
        for (int i = 0; i < length; i++) {
            sum += arr[i];
            maxPrefixSum = Math.max(maxPrefixSum, sum);
        }
        long postfixSum = 0, maxPostfixSum = 0;
        for (int i = length - 1; i >= 0; i--) {
            postfixSum += arr[i];
            maxPostfixSum = Math.max(maxPostfixSum, postfixSum);
        }
        long maxSum = Math.max(maxSubArraySum, maxPostfixSum + maxPrefixSum);
        if (sum > 0) {
            maxSum = Math.max(maxSum, sum * k);
            maxSum = Math.max(maxSum, maxPostfixSum + sum * (k - 2) + maxPrefixSum);
            maxSum = Math.max(maxSum, Math.max(sum * (k - 1) + maxPrefixSum, maxPostfixSum + sum * (k - 1)));
        }
        return (int) (maxSum % MODULO);
    }

    public long maxSubArraySum(int[] arr) {
        int length = arr.length;
        long newSum = arr[0], max = arr[0];
        for (int i = 1; i < length; i++) {
            newSum = Math.max(newSum + arr[i], arr[i]);
            max = Math.max(max, newSum);
        }
        max = Math.max(max, 0);
        return max;
    }
}

All Problems

All Solutions