Skip to content
Problem 689 · LeetCode 689(opens in a new tab)

Maximum Sum of 3 Non-Overlapping Subarrays

HardArrays

You are given an array of positive integers nums and a length k. Pick three windows, each exactly k values long, that do not share any position, so that the three window sums added together are as large as possible.

Return the starting indices of the three windows in increasing order. If several choices reach the same largest total, return the one that is smallest when compared index by index from the left.

Example 1

Input:  nums = [4, 1, 1, 3, 2, 5, 1], k = 2
Output: [0, 2, 4]
Why:    5 + 4 + 7 = 16; [0, 3, 5] also makes 16 but is larger
2 more examples and the constraints

Example 2

Input:  nums = [1, 1, 1, 1, 1, 1], k = 2
Output: [0, 2, 4]
Why:    the windows must tile the whole array

Example 3

Input:  nums = [9, 8, 1, 7, 3, 6, 2], k = 1
Output: [0, 1, 3]
Why:    with k = 1, take the three biggest values: 9, 8, 7

Constraints

  • 1 ≤ length of nums ≤ 20,000
  • 1 ≤ nums[i] < 65,536
  • 1 ≤ k ≤ length of nums ÷ 3 (rounded down)

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Fix the middle, scan both sidesO(n²)
  2. Best-left and best-right arraysO(n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 20,000 valuesVerdict
Fix the middle, scan both sidesO(n²)
Too slow at the limit
Best-left and best-right arraysO(n)
Best; a few linear passes

n = length of nums; m = n − k + 1 window starts.

Edge cases to test

  • The windows fill the array exactly. When the length is 3k, the only answer is [0, k, 2k].
  • All values equal. Every choice ties, so the answer is [0, k, 2k].
  • k = 1. The answer is the positions of the three largest values, earliest on ties.
  • Ties between different triples. Keep the earliest left start, then the earliest middle, then the earliest right.
  • Large sums. Three windows can total about 1.3 billion, which still fits in a 32-bit int.

Common mistakes

  • Using >= when filling left or > when filling right, which returns a later index on ties.
  • Letting the side windows touch the middle one: the left start must be at most j − k and the right start at least j + k.
  • Replacing the answer on an equal total, which moves the middle window later.

How to talk through it

  1. 1Clarify. Are values positive? How are ties broken? (Smallest index list, compared left to right.)
  2. 2Precompute window sums. A sliding window gives every k-length sum in O(n).
  3. 3Fix the middle. With j fixed, the left and right choices are independent; scanning them is O(n²).
  4. 4Precompute the sides. Best start to the left of each i and to the right of each i, with careful tie rules: O(n).
  5. 5Test out loud. Run a tie case like [4, 1, 1, 3, 2, 5, 1] with k = 2 and check [0, 2, 4] comes back.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?