K-Concatenation Maximum Sum Problem
Description
LeetCode Problem 1191.
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
Explanation:
The concatenated array is [1, 2, 1, 2, 1, 2].
The maximum-sum subarray is the entire array, with sum 1 + 2 + 1 + 2 + 1 + 2 = 9.
Example 2:
Input: arr = [1, -2, 1], k = 5
Output: 2
Explanation:
The concatenated array is [1, -2, 1, 1, -2, 1, 1, -2, 1, 1, -2, 1, 1, -2, 1].
The maximum-sum subarray is [1, 1], formed by the last element of one copy and the first element of the next copy, with sum 1 + 1 = 2.
Example 3:
Input: arr = [-1, -2], k = 7
Output: 0
Explanation:
Every element in the concatenated array is negative, so no subarray has a positive sum. Therefore, the maximum possible sum is 0.
Constraints:
1 <= arr.length <= 10^51 <= k <= 10^5-10^4 <= arr[i] <= 10^4
Solution
Approach: Prefix and Suffix Sums
k can be 10^5 and so can the array, so we cannot build the concatenated array. Instead we look at where the best subarray can sit. It is one of three shapes:
- Inside one copy. This is the classic maximum subarray (Kadane), where the empty subarray with sum
0is allowed. - Across one boundary: the best suffix of a copy followed by the best prefix of the next copy. This needs
k >= 2. - Across many copies: the best suffix, then all
k - 2copies in the middle in full, then the best prefix. The full copies only help if the array's totaltotalis positive.
A subarray that covers some copies fully and the total is not positive is never better than dropping those full copies, so these three cases are all we need.
One pass over arr gives everything. Keep the running prefix sum total, the largest prefix sum bestPre and the smallest prefix sum minPre (both start at 0 for the empty prefix). A subarray ending here has sum total - (some earlier prefix), so the best one is total - minPre; its maximum over the pass is the in-copy answer. At the end, the best suffix is total - minPre too: the whole array minus the smallest prefix.
Take the modulo only at the very end: compare the real sums first, then reduce. With k = 1 only the first case applies. The largest possible sum is about 10^5 * 10^9 = 10^14, which fits in a 64-bit integer.
Complexity: O(n) time and O(1) extra space.
class Solution:
def kConcatenationMaxSum(self, arr: List[int], k: int) -> int:
MOD = 10**9 + 7
total = 0 # sum of one copy
best_pre = 0 # largest prefix sum (the empty prefix counts)
min_pre = 0 # smallest prefix sum
best_sub = 0 # best subarray inside one copy (may be empty)
for x in arr:
total += x
best_pre = max(best_pre, total)
min_pre = min(min_pre, total)
best_sub = max(best_sub, total - min_pre)
if k == 1:
return best_sub % MOD
best_suf = total - min_pre # largest suffix sum
# Across one boundary: a suffix of one copy followed by a prefix of the next.
ans = max(best_sub, best_suf + best_pre)
if total > 0:
# Worth taking every copy in between in full.
ans = max(ans, best_suf + (k - 2) * total + best_pre)
return ans % MOD