data-structures · intermediate · ~15 min

Integer break

Maximize the product of parts that sum to n.

Challenge

Implement:

int integer_break(int n);

Break n (n >= 2) into a sum of at least two positive integers and return the maximum product of those parts.

Input format

n >= 2.

Output format

Maximum product.

Constraints

At least two parts.

Starter code

#include <stddef.h>
/* Break n (n>=2) into a sum of at least two positive integers; return the maximum product of those parts. */
int integer_break(int n){ (void)n; return 0; }

Common mistakes

Forgetting a factor may itself be left unbroken — compare max(j, dp[j]).

Edge cases to handle

n=2 -> 1 (1+1); n=4 -> 4 (2+2).

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.