Splitting a set or sequence into groups, subject to a constraint. Two very different families share the name.

Family 1: partition a set by value

Subset sum

Can a subset sum to exactly ?0/1 knapsack with values equal to weights.

bitset<MAXSUM + 1> dp;
dp[0] = 1;
for (int x : a) dp |= dp << x;
bool ok = dp[S];

with a bitset — the single most valuable optimisation in this family.

Equal-sum partition

Split into two subsets of equal sum. Feasible iff the total is even and is a reachable subset sum.

Minimum-difference partition

Minimise . Compute all reachable sums up to and take the largest:

for (int s = total / 2; s >= 0; s--)
    if (dp[s]) { ans = total - 2 * s; break; }

Partition into equal-sum subsets

NP-hard. For , bitmask DP: dp[mask] = (number of complete groups, current partial sum). For larger , backtracking with strong pruning — sort descending, skip duplicate values, and abandon a branch as soon as a group cannot be completed.

Balanced partition with huge values

Family 2: partition a sequence into contiguous segments

The array order is fixed; you choose where to cut.

— best cost for the first elements in segments.

for (int j = 1; j <= K; j++)
    for (int i = 1; i <= n; i++)
        for (int k = 0; k < i; k++)
            dp[i][j] = min(dp[i][j], dp[k][j-1] + cost(k, i));

naively. The optimisations:

ConditionTechniqueTime
is Monge (monotone argmin)D&C DP
linear in a parameterCHT
answer vs is convex, hugeAliens trick
segment length boundedmonotone queue
minimise the maximum segment costbinary search + greedy

Minimax partitioning — always check for this

“Split into segments minimising the largest segment sum” does not need DP. Binary search the answer and greedily cut whenever the running sum would exceed ; feasible iff the number of segments is .

bool feasible(long long X, int K) {
    int segs = 1; long long cur = 0;
    for (long long x : a) {
        if (x > X) return false;
        if (cur + x > X) { segs++; cur = 0; }
        cur += x;
    }
    return segs <= K;
}

— vastly simpler and faster than the DP. Recognising minimax objectives is worth a lot.

Family 3: integer partitions (counting)

In how many ways can be written as a sum of positive integers, order irrelevant? — the partition function .

The DP is exactly coin change combinations with coins : .

Faster: Euler’s pentagonal number theorem gives

which is — enough for .

Variants: partitions into distinct parts, into odd parts (equinumerous, by Euler), into at most parts, with parts of bounded size. All are small modifications to the coin-change DP or to the generating function.

See also: Knapsack · Subset Sum · Generating Functions · Binary Search on the Answer