All problems
0572MediumMathDynamic Programming

Cut the Rod for the Largest Product

Tracked in this browser only
Write code

Trains the technique from

LeetCode 343Integer Break

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A supplier holds a single rod of length n centimetres and has to saw it into two or more pieces. Every piece must be a whole number of centimetres long, and the piece lengths have to add up to exactly n. Pieces may repeat a length, and their order does not matter.

The pieces are then sold as a set priced at the product of their lengths. Return the largest product any allowed set of pieces can reach.

Examples

Example 1

Input
n = 7
Output
12

Cutting the rod into pieces of 3 and 4 gives lengths that add to 7 and a product of 12.

Example 2

Input
n = 4
Output
4

Cutting into 2 and 2 gives a product of 4, and the two pieces add up to 4.

Example 3

Input
n = 43
Output
6377292

One allowed set is thirteen pieces of 3 and one piece of 4, which add up to 43 and multiply to 6377292.

Constraints

  • 2 <= n <= 58
  • The answer never exceeds 1549681956.

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def integer_break(n: int) -> int:
Java
public int integerBreak(int n)
September 7
Apply