We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Maximum Strength of K Disjoint Subarrays

Number: 3313

Difficulty: Hard

Paid? No

Companies: DE Shaw


Problem Description

Given an integer array nums and a positive odd integer k, select exactly k disjoint non-empty subarrays (in order) from nums. The strength of the chosen subarrays is calculated as:   strength = k * sum(sub₁) - (k - 1) * sum(sub₂) + (k - 2) * sum(sub₃) - … - 2 * sum(subₖ₋₁) + 1 * sum(subₖ). Return the maximum possible strength obtainable.


Key Insights

  • The problem is similar to selecting k disjoint subarrays; however, each subarray’s contribution is multiplied by a weight that alternates in sign.
  • The weight for the j-th subarray is defined as: weight[j] = (k - j + 1) when j is odd and - (k - j + 1) when j is even.
  • A dynamic programming approach can be used, tracking the best possible strength when selecting j subarrays up to the current index.
  • Similar to Kadane’s algorithm, we maintain an ongoing (local) best weighted sum when extending the current subarray or starting a new one.
  • By iterating through nums and updating two arrays – one for the best ending an ongoing subarray and one for the overall best result for j selections – we can achieve an O(n*k) solution.

Space and Time Complexity

Time Complexity: O(n * k), where n is the length of the array and k is the number of subarrays. Space Complexity: O(k), since we only maintain dp arrays of size k+1.


Solution

We define two arrays:

  1. curr[j] – the maximum strength value when we are in the middle of forming the j-th subarray (ending at the current element).
  2. best[j] – the best total strength after finishing j subarrays up to the current position. Initialize best[0] = 0 (using zero subarrays we contribute nothing) and all other positions as very small (negative infinity).

For every element in nums, we update the dp arrays for each j from 1 to k. There are two choices for the j-th subarray at index i: • Continue the current subarray: curr[j] = curr[j] + weight[j] * nums[i] • Start a new j-th subarray at index i using the best answer from j-1 finished subarrays: curr[j] = best[j-1] + weight[j]*nums[i] Take the maximum of these two and then update best[j] accordingly.

The final answer is found in best[k] after processing the entire array.


Code Solutions

import math

def maxStrength(nums, k):
    n = len(nums)
    # Precompute the weights based on subarray index.
    # For 1-indexed j: weight = k - j + 1 if j is odd, else - (k - j + 1)
    weights = [0] * (k + 1)
    for j in range(1, k + 1):
        if j % 2 == 1:
            weights[j] = k - j + 1
        else:
            weights[j] = -(k - j + 1)
    
    # Initialize dp arrays.
    best = [-math.inf] * (k + 1)
    curr = [-math.inf] * (k + 1)
    best[0] = 0  # Using zero subarrays gives 0
    # Iterate over each number in the array.
    for num in nums:
        # Process in order from 1 to k.
        for j in range(1, k + 1):
            # Option to start a new subarray j at current num.
            option_start = best[j - 1] + weights[j] * num
            # Option to extend the current subarray j.
            option_extend = curr[j] + weights[j] * num
            curr[j] = max(option_start, option_extend)
            # Update best value for j completed subarrays.
            best[j] = max(best[j], curr[j])
    return best[k]

# Example usage:
print(maxStrength([1, 2, 3, -1, 2], 3))  # Expected output: 22
← Back to All Questions