Maximum Sum of Two Non-Overlapping Subarrays - Problem

Given an integer array nums and two integers firstLen and secondLen, find two non-overlapping subarrays that maximize their combined sum.

The first subarray must have exactly firstLen elements, and the second subarray must have exactly secondLen elements. These subarrays can appear in any order in the original array - the shorter one could come before or after the longer one.

Goal: Return the maximum possible sum of elements from both subarrays combined.

Example: For array [0,6,5,2,2,5,1,9,4] with firstLen=1 and secondLen=2, we could choose subarray [9] (length 1) and [6,5] (length 2) for a total sum of 9 + 11 = 20.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [0,6,5,2,2,5,1,9,4], firstLen = 1, secondLen = 2
โ€บ Output: 20
๐Ÿ’ก Note: One choice of subarrays is [9] with length 1, and [6,5] with length 2. The sum is 9 + 6 + 5 = 20. We can also choose [6] and [2,5] for sum 6 + 2 + 5 = 13, but 20 is better.
example_2.py โ€” Different Lengths
$ Input: nums = [3,8,1,3,2,1,8,9,0], firstLen = 3, secondLen = 2
โ€บ Output: 29
๐Ÿ’ก Note: One choice is [3,8,1] with length 3, and [8,9] with length 2. Sum = (3+8+1) + (8+9) = 12 + 17 = 29. Another choice is [1,8,9] and [3,8] for sum 18 + 11 = 29.
example_3.py โ€” Edge Case Small Array
$ Input: nums = [2,1,5,6,0,9,5,0,3,8], firstLen = 4, secondLen = 3
โ€บ Output: 31
๐Ÿ’ก Note: Choose [5,6,0,9] with length 4 (sum=20) and [0,3,8] with length 3 (sum=11). Total sum = 20 + 11 = 31.

Visualization

Tap to expand
Hotel Rooms (Array Elements)$6$5Group 1: $11$9Group 2: $9Total Revenue: $11 + $9 = $20๐Ÿจ Optimization Strategy1. Try placing Group 1 first, find best Group 2 position2. Try placing Group 2 first, find best Group 1 position3. Use sliding windows for efficient calculation๐ŸŽฏ Key: Track maximum revenue in each direction!
Understanding the Visualization
1
Setup
We have a row of rooms (array elements) and two groups needing contiguous rooms
2
Constraint
Groups cannot overlap - they need separate, non-overlapping room blocks
3
Goal
Maximize total revenue (sum of both subarrays) by optimal placement
4
Strategy
Use sliding windows to efficiently find the best placement for both groups
Key Takeaway
๐ŸŽฏ Key Insight: By maintaining the maximum subarray sum seen so far in one direction while sliding the window in the other direction, we can find the optimal combination in O(n) time instead of checking all O(nยฒ) possibilities.

Time & Space Complexity

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

O(n) to compute all subarray sums, O(nยฒ) to check all combinations

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Storing all precomputed subarray sums

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค firstLen, secondLen โ‰ค 1000
  • 2 โ‰ค nums.length โ‰ค 1000
  • firstLen + secondLen โ‰ค nums.length
  • 0 โ‰ค nums[i] โ‰ค 1000
  • The two subarrays must be non-overlapping
Asked in
Amazon 45 Google 32 Microsoft 28 Meta 22 Apple 18
47.0K Views
Medium-High Frequency
~18 min Avg. Time
2.2K 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