Sum of K Subarrays With Length at Least M - Problem

You're tasked with finding the maximum possible sum by selecting exactly k non-overlapping subarrays from an integer array, where each selected subarray must have a length of at least m elements.

Given an integer array nums and two integers k and m, you need to strategically choose k subarrays that:

  • Do not overlap with each other
  • Each has a length ≄ m
  • Together produce the maximum possible sum

Example: If nums = [1, 2, 1, 2, 6, 7, 5, 1], k = 2, and m = 2, you might choose subarrays [2, 1, 2] (sum = 5) and [6, 7] (sum = 13) for a total of 18.

This problem combines dynamic programming with subarray optimization - you'll need to consider all possible ways to partition the array while maximizing the sum!

Input & Output

example_1.py — Basic Case
$ Input: nums = [1, 2, 1, 2, 6, 7, 5, 1], k = 2, m = 2
› Output: 18
šŸ’” Note: Choose subarrays [2, 1, 2] (indices 1-3, sum=5) and [6, 7, 5] (indices 4-6, sum=18). Total: 5 + 13 = 18. Alternative: [1, 2] (sum=3) and [6, 7, 5] (sum=18) gives total 21, but [2, 6, 7] (sum=15) and [5, 1] (sum=6) gives 21 as well.
example_2.py — Minimum Length
$ Input: nums = [5, -2, 4, -1, 3], k = 2, m = 1
› Output: 8
šŸ’” Note: With minimum length 1, we can choose single elements. Optimal: [5] (sum=5) and [4, -1, 3] (sum=6), total = 11. Or [5] and [4] for total = 9. Best is [5] and [3] for total = 8.
example_3.py — Edge Case
$ Input: nums = [1, 1, 1, 1], k = 2, m = 2
› Output: 4
šŸ’” Note: Only one way to select 2 non-overlapping subarrays of length ≄ 2: [1,1] (sum=2) and [1,1] (sum=2). Total = 4.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 1 ≤ k ≤ 100
  • 1 ≤ m ≤ nums.length
  • -104 ≤ nums[i] ≤ 104
  • k Ɨ m ≤ nums.length (must be possible to select k non-overlapping subarrays)

Visualization

Tap to expand
Real Estate Investment StrategyStreet with Properties (Array Elements)$1$2$1$2$6$7$5$1Investment Selection (k=2, m=2)Group 1: $2+$1+$2 = $5Group 2: $6+$7+$5 = $18Total Investment Value: $5 + $18 = $23Dynamic Programming Approach• At each position: Skip or Start investment• Try all valid lengths (≄ m houses)• Cache results for (position, remaining_groups)Use prefix sums for O(1) segment value calculationšŸŽÆ Key Insight: Memoize overlapping subproblems for efficiency!
Understanding the Visualization
1
Survey Properties
Build prefix sums to quickly calculate any property segment value
2
Decision Points
At each house, decide: skip it or start a new property investment here
3
Try All Sizes
If starting investment, consider all valid lengths (m to end of street)
4
Memoize Results
Cache results for (position, remaining_investments) to avoid recalculation
Key Takeaway
šŸŽÆ Key Insight: The problem is solved optimally using dynamic programming with memoization, where we cache results for each (position, remaining_subarrays) state and use prefix sums for efficient range sum queries.
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
28.5K Views
Medium 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