Talentd
FleetCode
Make and follow sheets.Sign in

Sum of K Subarrays With Length at Least M solution and explanation

MediumArrayDynamic ProgrammingPrefix Sum3 min read26.0% acceptanceUpdated 24 Mar 2026
3473. Sum of K Subarrays With Length at Least M | Top-Down | Bottom Up DP | Bottom Up OptimisedAryan Mittal · 29:26 · 3K viewsWatch on YouTube
4 more videos on this problem
  1. 3473. Sum of K Subarrays With Length at Least M | Top-Down | Bottom Up DP | Bottom Up OptimisedAryan Mittal · 29:26 · 3K views
  2. 3473. Sum of K Subarrays With Length at Least M (Leetcode Medium)Programming Live with Larry · 2:43:18 · 456 views
  3. Sum of K Subarrays With Length at Least M (Leetcode Weekly 439)Soumya Bhattacharjee · 9:30 · 251 views
  4. leetcode 3473 Sum of K Subarrays With Length at Least M | DPCode-Yao · 26:11 · 168 views
  5. L3473: Sum of K Subarrays With Length at Least M | Knapsack DP | Full ExplanationSoftware Is Life · 29:53 · 110 views

The problem

You are given an integer array nums and two integers, k and m.

Return the maximum sum of k non-overlapping subarrays of nums, where each subarray has a length of at least m.

Example 1:

Input: nums = [1,2,-1,3,3,4], k = 2, m = 2

Output: 13

Explanation:

The optimal choice is:

  • Subarray nums[3..5] with sum 3 + 3 + 4 = 10 (length is 3 >= m).
  • Subarray nums[0..1] with sum 1 + 2 = 3 (length is 2 >= m).

The total sum is 10 + 3 = 13.

Example 2:

Input: nums = [-10,3,-1,-2], k = 4, m = 1

Output: -10

Explanation:

The optimal choice is choosing each element as a subarray. The output is (-10) + 3 + (-1) + (-2) = -10.

Constraints:

  • 1 <= nums.length <= 2000
  • -104 <= nums[i] <= 104
  • 1 <= k <= floor(nums.length / m)
  • 1 <= m <= 3

Approach overview

Problem Overview: You are given an array and must choose exactly k non-overlapping subarrays. Each chosen subarray must have length at least m. The goal is to maximize the total sum of all selected subarrays.

Approach 1: Brute Force Enumeration (Exponential)

Generate all possible subarrays of length ≥ m and try every combination of k non‑overlapping ones. For each valid set, compute the total sum and track the maximum. Prefix sums can speed up subarray sum calculation, but the number of combinations still grows exponentially. Time complexity is roughly O(n^k) in the worst case with O(n) auxiliary space for prefix sums. This approach only works for very small arrays and mainly helps understand the search space.

Approach 2: Dynamic Programming with Prefix Sums (O(n²k))

Use prefix sum to compute any subarray sum in O(1). Define dp[i][j] as the maximum sum using j valid subarrays considering the first i elements. For every ending index i, iterate over all possible starting points t where the subarray length is at least m. Update dp[i][j] = max(dp[i][j], dp[t-1][j-1] + sum(t..i)). This guarantees non-overlapping segments because the previous state ends before t. Time complexity is O(n²k) and space complexity is O(nk). It works but becomes slow when n grows.

Approach 3: Optimized DP with Rolling Best (O(nk))

The inner loop from the previous approach can be optimized by maintaining the best candidate transition. Rearranging the recurrence: dp[i][j] = prefix[i] + max(dp[t-1][j-1] - prefix[t-1]), where t ≤ i-m+1. As you iterate i, track the maximum value of dp[t-1][j-1] - prefix[t-1] for all valid starts. This eliminates the need to scan every possible t. Each index is processed once per k, resulting in O(nk) time and O(nk) space. This technique combines array iteration with dynamic programming and prefix sums.

Recommended for interviews: The optimized dynamic programming approach is what interviewers typically expect. Starting with the O(n²k) DP shows you understand the state transition. Reducing it to O(nk) by maintaining a running best value demonstrates strong DP optimization skills.

The written solution for this problem is still being prepared. Try it yourself in the meantime.

Try solving it yourself

Complexity analysis

ApproachTimeSpaceWhen to Use
Brute Force EnumerationExponential (≈O(n^k))O(n)Understanding the problem or verifying small test cases
DP with Prefix SumsO(n²k)O(nk)Straightforward DP formulation when constraints are moderate
Optimized DP with Running BestO(nk)O(nk)Optimal solution for large arrays; typical interview expectation

Frequently asked questions

Is Sum of K Subarrays With Length at Least M easy or hard?

The problem is typically rated Medium because the core idea requires dynamic programming with careful state transitions. Recognizing how to optimize the naive O(n²k) DP into an O(nk) solution using prefix sums and a running maximum is the key difficulty.

Sum of K Subarrays With Length at Least M Python/Java solution

Most implementations follow the optimized DP pattern. Compute prefix sums, maintain a dp table for k selections, and track a running best transition value while iterating the array. The same logic translates easily to Python, Java, C++, and Go with O(nk) complexity.

How to solve Sum of K Subarrays With Length at Least M in O(nk)?

Use prefix sums to compute subarray sums quickly and define dp[i][j] as the best sum using j subarrays up to index i. While scanning the array, maintain the best value of dp[t-1][j-1] minus prefix[t-1] for valid start positions t ≤ i-m+1. This removes the inner loop over starting indices and reduces the complexity to O(nk).

What is the best approach for Sum of K Subarrays With Length at Least M?

The most efficient approach uses dynamic programming combined with prefix sums. Maintain dp[i][j] as the best sum using j subarrays within the first i elements. By keeping a running maximum of dp[t-1][j-1] - prefix[t-1], the transition can be computed in O(1), leading to an overall O(nk) time complexity.

Is Sum of K Subarrays With Length at Least M asked at Google/Amazon/Meta?

Problems involving selecting multiple non-overlapping subarrays with dynamic programming appear frequently in interviews at companies like Google, Amazon, and Meta. Variants such as maximum sum of k subarrays or subarrays with minimum length constraints are common DP interview questions.

What data structure is used in Sum of K Subarrays With Length at Least M?

The main structures are arrays for prefix sums and a dynamic programming table. Prefix sums allow constant-time range sum queries, while the DP table tracks the best result for choosing different numbers of subarrays.

What is the time complexity of Sum of K Subarrays With Length at Least M?

The optimized solution runs in O(nk) time where n is the array length and k is the number of required subarrays. Each element is processed once for every DP layer. Space complexity is O(nk) for storing the dynamic programming states.

Comments

Now write it yourselfClose this page and solve Sum of K Subarrays With Length at Least M in the editor, without looking.
Practise Sum of K Subarrays With Length at Least M