K-Concatenation Maximum Sum - Problem
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 109 + 7.
Input & Output
Example 1 — Basic Positive Array
$
Input:
arr = [1,2], k = 3
›
Output:
9
💡 Note:
The concatenated array is [1,2,1,2,1,2]. The maximum subarray sum is 1+2+1+2+1+2 = 9 (taking the entire array).
Example 2 — Mixed Values
$
Input:
arr = [1,-2,1], k = 5
›
Output:
2
💡 Note:
The maximum subarray sum in a single array is max(1, -2, 1, 1-2, -2+1, 1-2+1) = 1. Since total sum is 0, we don't benefit from repetitions.
Example 3 — Single Element
$
Input:
arr = [-1], k = 2
›
Output:
0
💡 Note:
All elements are negative, so the maximum subarray sum is 0 (empty subarray).
Constraints
- 1 ≤ arr.length ≤ 105
- 1 ≤ k ≤ 105
- -104 ≤ arr[i] ≤ 104
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array [1,2] with k=3 repetitions
2
Process
Analyze patterns without full concatenation
3
Output
Maximum subarray sum is 9
Key Takeaway
🎯 Key Insight: We can solve this efficiently by analyzing three patterns instead of creating the full concatenated array
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code