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
Maximum Sum of 3 Non-Overlapping Subarrays INPUT nums array (k = 2) 0 1 2 3 4 5 6 7 1 2 1 2 6 7 5 1 Subarray 1 (sum=3) Subarray 2 (sum=8) Subarray 3 (sum=12) Input Parameters nums = [1,2,1,2,6,7,5,1] k = 2 Find 3 non-overlapping subarrays All Window Sums (k=2) [3, 3, 3, 8, 13, 12, 6] sums[i] = nums[i] + nums[i+1] ALGORITHM STEPS 1 Compute Window Sums Calculate sum for each k-window sums = [3,3,3,8,13,12,6] 2 Build Left Array Best left subarray ending at i left = [0,0,0,3,4,4,4] 3 Build Right Array Best right subarray starting at i right = [4,4,4,4,4,5,6] 4 Find Optimal Middle Try each middle position j For j=3: left[1]=0, mid=3, right[5]=5 Total = 3 + 8 + 12 = 23 (max) Time: O(n) | Space: O(n) FINAL RESULT Selected Subarrays 1 2 1 2 6 7 5 1 Subarray Breakdown idx 0: [1,2] = 3 idx 3: [2,6] = 8 idx 5: [7,5] = 12 OUTPUT [0, 3, 5] Starting indices Maximum Total Sum 3 + 8 + 12 = 23 Key Insight: Use Dynamic Programming with precomputed left and right optimal arrays. For each possible middle subarray position j, we can instantly find the best left subarray (ending before j) and best right subarray (starting after j+k). This reduces the problem from O(n^3) to O(n). TutorialsPoint - Maximum Sum of 3 Non-Overlapping Subarrays | Optimal DP Solution
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