Maximum Strength of K Disjoint Subarrays

You are given an array of integers nums with length n, and a positive odd integer k.

Select exactly k disjoint subarrays sub1, sub2, ..., subk from nums such that the last element of subi appears before the first element of sub{i+1} for all 1 <= i <= k - 1. The goal is to maximize their combined strength.

The strength of the selected subarrays is defined as:

strength = k * sum(sub1) - (k - 1) * sum(sub2) + (k - 2) * sum(sub3) - ... - 2 * sum(sub{k-1}) + sum(subk)

where sum(subi) is the sum of the elements in the i-th subarray.

Return the maximum possible strength that can be obtained from selecting exactly k disjoint subarrays from nums.

Note that the chosen subarrays don't need to cover the entire array.

Example 1
Inputnums = [1,2,3,-1,2], k = 3
Output22
The best selection is nums[0..2], nums[3..3], and nums[4..4], giving 3 * (1 + 2 + 3) - 2 * (-1) + 2 = 22.
Example 2
Inputnums = [12,-2,-2,-2,-2], k = 5
Output64
The only possible selection is five single-element subarrays, giving 5 * 12 - 4 * (-2) + 3 * (-2) - 2 * (-2) + (-2) = 64.

Constraints

  • 1 <= n <= 10^4
  • -10^9 <= nums[i] <= 10^9
  • 1 <= k <= n
  • 1 <= n * k <= 10^6
  • k is odd.

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate