Maximum Strength of K Disjoint Subarrays - Problem

You're given an array of integers nums with length n, and a positive odd integer k. Your mission is to strategically select exactly k disjoint subarrays to maximize their combined strength.

Key Rules:

  • The subarrays must be disjoint (non-overlapping)
  • They must appear in order: the last element of subarray i comes before the first element of subarray i+1
  • You don't need to use every element in the original array

Strength Formula:
The strength alternates between positive and negative coefficients:
strength = k × sum(sub₁) - (k-1) × sum(sub₂) + (k-2) × sum(sub₃) - ... + sum(subₖ)

For example, with k=3: 3×sum(sub₁) - 2×sum(sub₂) + 1×sum(sub₃)

Return the maximum possible strength you can achieve by optimally selecting k disjoint subarrays.

Input & Output

example_1.py — Basic Example
$ Input: nums = [1, 2, 3, -1, 2], k = 3
Output: 22
💡 Note: Select subarrays [1, 2], [3], [-1, 2] with strength = 3×(1+2) - 2×(3) + 1×(-1+2) = 9 - 6 + 1 = 4. But optimal is [1, 2, 3], [-1], [2] giving 3×6 - 2×(-1) + 1×2 = 18 + 2 + 2 = 22.
example_2.py — Negative Numbers
$ Input: nums = [-1, -2, -3], k = 1
Output: -1
💡 Note: With k=1, we need exactly one subarray. The best choice is [-1] with strength = 1×(-1) = -1, which is better than [-2] or [-3] or any longer subarray.
example_3.py — Mixed Values
$ Input: nums = [5, -3, 5], k = 3
Output: 13
💡 Note: Select subarrays [5], [-3], [5] with strength = 3×5 - 2×(-3) + 1×5 = 15 + 6 - 2 = 19. Wait, that's wrong calculation. Actually: 3×5 - 2×(-3) + 1×5 = 15 + 6 + 5 = 26. But we need to verify this is the optimal solution.

Constraints

  • 1 ≤ n ≤ 104
  • 1 ≤ k ≤ n
  • k is odd
  • -109 ≤ nums[i] ≤ 109
  • The subarrays must be disjoint and in order

Visualization

Tap to expand
Strategic Subarray Selection VisualizationInput: [1, 2, 3, -1, 2], k=3123-12Optimal Strategy: 3 subarrays with alternating coefficientsSubarray 1 (coeff = +3):[1,2,3]3 × 6 = 18Subarray 2 (coeff = -2):[-1]-2 × (-1) = 2Subarray 3 (coeff = +1):[2]1 × 2 = 2Total Strength = 18 + 2 + 2 = 22Dynamic Programming Approach:• State: dp[position][subarrays_used] = max strength• Transition: Include current element or start new subarray• Complexity: O(n²×k) time, O(n×k) space
Understanding the Visualization
1
Identify Opportunities
Scan the array to identify potential high-value subarrays
2
Apply Strategy
Use DP to systematically try different combinations
3
Calculate Weighted Impact
Apply alternating coefficients: positive for odd positions, negative for even
4
Optimize Selection
Choose the combination that maximizes total strength
Key Takeaway
🎯 Key Insight: Use dynamic programming to systematically explore all valid combinations while efficiently handling the alternating coefficient pattern in the strength calculation.
Asked in
Google 28 Meta 22 Amazon 18 Microsoft 15
27.4K Views
Medium Frequency
~35 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