Maximum Sum of 3 Non-Overlapping Subarrays - Problem

You're given an integer array nums and an integer k. Your task is to find three non-overlapping subarrays of length k that have the maximum total sum.

Return the result as a list of starting indices (0-indexed) of these three subarrays. If multiple valid answers exist with the same maximum sum, return the lexicographically smallest one.

Key constraints:

  • The three subarrays cannot overlap
  • Each subarray must have exactly k elements
  • You need the indices of where each subarray starts
  • Prefer smaller indices when there are ties

Example: For nums = [1,2,1,2,6,7,5,1] and k = 2, the answer is [0,3,5] because subarrays [1,2], [2,6], and [7,5] give the maximum sum of 22.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [1,2,1,2,6,7,5,1], k = 2
โ€บ Output: [0, 3, 5]
๐Ÿ’ก Note: Subarrays [1,2] at index 0 (sum=3), [2,6] at index 3 (sum=8), and [7,5] at index 5 (sum=12) give maximum total sum of 23.
example_2.py โ€” Tie Breaking
$ Input: nums = [1,2,1,2,1,2,1,2,1], k = 2
โ€บ Output: [0, 2, 4]
๐Ÿ’ก Note: Multiple combinations give the same sum, but [0,2,4] is lexicographically smallest. Subarrays [1,2], [1,2], [1,2] each sum to 3, total = 9.
example_3.py โ€” Minimum Length
$ Input: nums = [4,3,2,1,7,6], k = 1
โ€บ Output: [0, 4, 5]
๐Ÿ’ก Note: With k=1, we pick individual elements. Elements 4, 7, 6 at indices [0,4,5] give maximum sum of 17.

Constraints

  • 1 โ‰ค nums.length โ‰ค 2 ร— 104
  • 1 โ‰ค nums[i] โ‰ค 106
  • 1 โ‰ค k โ‰ค floor(nums.length / 3)
  • The array must have space for at least 3 non-overlapping subarrays of length k

Visualization

Tap to expand
๐ŸŽญ Theater Seating OptimizationTheater Layout (k=2 seat blocks)$3 Block$3 Block$8 Block$12 BlockOptimization StrategyLeft PrecomputeBest left block foreach positionMiddle IterateTry each middleblock positionRight PrecomputeBest right block foreach positionPerformance Benefits๐Ÿš€ Time: O(n) - Linear scan instead of cubic search๐Ÿ’พ Space: O(n) - Precomputation arrays๐ŸŽฏ Smart: Avoid recalculating optimal left/right positions๐Ÿ’ก Key insight: Precompute optimal boundaries to make middle selection O(n)
Understanding the Visualization
1
Calculate Block Values
Determine the total value of each possible k-seat block using sliding window
2
Precompute Left Options
For each position, know the best left block that doesn't interfere
3
Precompute Right Options
For each position, know the best right block that doesn't interfere
4
Optimize Middle Block
Try each middle block and combine with precomputed optimal left/right blocks
Key Takeaway
๐ŸŽฏ Key Insight: By precomputing the optimal left and right subarray positions for each index, we transform a cubic time problem into a linear one. The middle position iteration becomes the only variable, while left and right positions are optimally determined through dynamic programming.
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
42.8K Views
High Frequency
~18 min Avg. Time
1.5K 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