K-Concatenation Maximum Sum - Problem
K-Concatenation Maximum Sum challenges you to find the maximum subarray sum in an array that has been repeated k times consecutively.

Given an integer array arr and an integer k, you need to:
1. Create a new array by repeating arr exactly k times
2. Find the maximum sum of any contiguous subarray in this concatenated array
3. Return the result modulo 109 + 7

Example: If arr = [1, 2] and k = 3, the concatenated array becomes [1, 2, 1, 2, 1, 2]. The maximum subarray sum would be the sum of the entire array: 9.

Note: Empty subarrays are allowed (sum = 0), so the answer is never negative.

Input & Output

example_1.py โ€” Basic Case
$ Input: arr = [1, 2], k = 3
โ€บ Output: 9
๐Ÿ’ก Note: The concatenated array is [1,2,1,2,1,2]. The maximum subarray sum is achieved by taking the entire array: 1+2+1+2+1+2 = 9.
example_2.py โ€” Mixed Values
$ Input: arr = [1, -2, 1], k = 5
โ€บ Output: 2
๐Ÿ’ก Note: The concatenated array is [1,-2,1,1,-2,1,1,-2,1,1,-2,1,1,-2,1]. The maximum subarray is [1,1] which appears multiple times, giving sum = 2.
example_3.py โ€” All Negative
$ Input: arr = [-1, -2], k = 7
โ€บ Output: 0
๐Ÿ’ก Note: All elements are negative, so the optimal strategy is to choose an empty subarray with sum 0.

Visualization

Tap to expand
K-Concatenation Maximum Sum VisualizationExample: arr = [1, -2, 1], k = 5Concatenated: [1,-2,1,1,-2,1,1,-2,1,1,-2,1,1,-2,1]Step 1: Analyze original array [1, -2, 1]1-21max_subarray = 1, max_prefix = 1, max_suffix = 1, total = 0Step 2: Check double array [1,-2,1,1,-2,1]1-211-21max_double = 2 (from [1,1])Step 3: Since total_sum = 0, no benefit from middle repetitionsAnswer = max_double = 2Key Insight Visualization:Suffix(k-2) Full ArraysPrefixโ† Only when total > 0Time: O(n) | Space: O(1) - Independent of k!
Understanding the Visualization
1
Analyze Single Array
Use Kadane's algorithm to find max subarray, plus calculate best prefix and suffix sums
2
Check Double Concatenation
Find the maximum subarray in exactly 2 concatenated arrays to handle boundary-crossing cases
3
Optimize with Middle Repetitions
If the total array sum is positive and k > 2, add (k-2) complete arrays between the optimal suffix and prefix
Key Takeaway
๐ŸŽฏ Key Insight: The maximum subarray in k concatenations can span at most 2 complete arrays plus middle repetitions, allowing us to solve in O(n) time regardless of k value!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O((n*k)ยฒ)

We have n*k elements and check all O((n*k)ยฒ) subarrays

n
2n
โœ“ Linear Growth
Space Complexity
O(n*k)

Store the entire concatenated array of size n*k

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค arr.length โ‰ค 105
  • -104 โ‰ค arr[i] โ‰ค 104
  • 1 โ‰ค k โ‰ค 105
  • Answer must be returned modulo 109 + 7
Asked in
Google 23 Amazon 18 Meta 12 Microsoft 8
26.8K Views
Medium-High Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen