Medium
ArrayDynamic Programming
Updated Sep 2026

Partition Array for Maximum Sum

Asked at TikTok

Problem

Partition Array for Maximum Sum asks you to split an array into contiguous pieces of length at most k, replace every element of each piece with that piece's maximum, and return the largest possible total. It is a 1D DP where each state tries every possible length for the last piece.

Asked At

CompanyDifficulty
TikTokMediumView all TikTok questions →

How to Think About It

1.

Greedy choices (always take pieces of length k, or always extend around big numbers) fail on small counterexamples. You need to try the options.

2.

Key insight: let dp[i] be the best total for the prefix of length i. The last piece ends at index i - 1 and has some length len from 1 to k.

3.

For each len, the piece contributes max(arr[i-len..i-1]) * len, so dp[i] = max over len of dp[i - len] + maxInPiece * len.

4.

Compute the running maximum while extending len backward, so each state costs O(k).

5.

Walkthrough for [1,15,7,9,2,5,10], k = 3: the best split is [1,15,7] [9] [2,5,10] -> 15*3 + 9 + 10*3 = 84.

Optimal Approach

Step 1: dp = [0] * (n + 1).
Step 2: For i from 1 to n:
cur = 0
For len from 1 to min(k, i):
cur = max(cur, arr[i - len])
dp[i] = max(dp[i], dp[i - len] + cur * len)
Step 3: Return dp[n].

Time: O(n * k). Space: O(n).

What Trips People Up in Real Interviews

1.

Recomputing the piece maximum from scratch for each length — that adds another factor of k.

2.

Off-by-one with 1-indexed dp versus 0-indexed arr. The last piece for dp[i] covers arr[i-len .. i-1].

3.

Trying a greedy split. Always ask yourself for a counterexample before committing to greedy on partition problems.

4.

Forgetting to cap len at i for the first few positions.

Solution Code

def maxSumAfterPartitioning(arr, k):
    n = len(arr)
    dp = [0] * (n + 1)
    for i in range(1, n + 1):
        cur = 0
        for length in range(1, min(k, i) + 1):
            cur = max(cur, arr[i - length])
            dp[i] = max(dp[i], dp[i - length] + cur * length)
    return dp[n]

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Partition Array for Maximum Sum problem?

Partition Array for Maximum Sum asks you to split an array into contiguous pieces of length at most `k`, replace every element of each piece with that piece's maximum, and return the largest possible total. It is a 1D DP where each state tries every possible length for the last piece.

How do you solve Partition Array for Maximum Sum?

The optimal approach is described in detail above, including step-by-step walkthroughs, complexity analysis, and solution code in Python. Scroll up to the "Optimal Approach" section.

What companies ask Partition Array for Maximum Sum?

Partition Array for Maximum Sum is asked at TikTok. It is a medium difficulty problem.

What are common mistakes on Partition Array for Maximum Sum?
  • Recomputing the piece maximum from scratch for each length — that adds another factor of `k`.
  • Off-by-one with 1-indexed `dp` versus 0-indexed `arr`. The last piece for `dp[i]` covers `arr[i-len .. i-1]`.
  • Trying a greedy split. Always ask yourself for a counterexample before committing to greedy on partition problems.
  • Forgetting to cap `len` at `i` for the first few positions.