Integer Break
Integer Break is LeetCode 343 (Medium). Split a whole number n into a sum of at least two positive whole numbers, and make the product of those parts as large as possible. Return that product.
nruns from 2 to 58. The largest answer, for 58, is 1,549,681,956, which still fits in a 32-bit int.
- 2 <= n <= 58
Intuition
Ask which part sizes can ever appear in a best split.
- No part of 5 or more: a part
p ≥ 5can be cut into2andp − 2, and2(p − 2) > pwheneverp > 4, so cutting it always helps. - No part of 1: adding a 1 to a neighbour part raises the product, since
x + 1 > x × 1. - Threes beat twos: three 2s make 6 with product 8, while two 3s make 6 with product 9, so a best split has at most two 2s.
That leaves one rule: cut off 3s while more than 4 is left, then multiply by what remains (2, 3 or 4). A leftover 4 stays as 2 × 2, because 3 × 1 is worse.
"Split a number into parts and maximise a product" can be done with a DP over smaller totals, but a short argument about which part sizes can ever be best turns it into a formula.
Approach
Before reading on, find the best split for every n from 2 to 10 by hand. Which part sizes keep showing up? What happens to a part of 5 if you cut it into 2 and 3?
Two ways to find the best split
Take 3s while more than 4 is left, then multiply by the rest.
- Fast and tiny: a few lines, no table.
- Needs the proof: be ready to explain why 3 is the best part size.
best[i] is the largest product from splitting i. Try every first part j, and either keep the rest whole or split it further.
- No insight needed: the standard way to show the DP pattern.
- Adapts: still works if parts get a price table, as in rod cutting.
The steps, code and live demo use the rule about threes; the table version, which needs no proof, follows the demo.
Handle 2 and 3 first
At least one cut is required, so 2 must become 1 + 1 and 3 must become 1 + 2. Return n − 1. These are the only values where the forced cut makes the product smaller than n itself.
Cut off threes while more than 4 is left
Start with product = 1. While n > 4, multiply product by 3 and take 3 from n. Each such cut leaves at least 2, so it never forces a part of 1.
Multiply by what is left
The loop stops with 2, 3 or 4 remaining. Each is best kept as it is: 2 and 3 cannot be split usefully, and 4 as a single factor equals 2 × 2. Return product × n.
Why this split is the best one
Any split with a part of 1 or a part of 5 or more can be improved, as can one with three 2s. So a best split uses only 2s and 3s with at most two 2s, and for each n there is exactly one such split. The loop builds that split.
Code in Python, C++ and Java
Table of best products
best[i] is the largest product from splitting i into at least two parts. For each first part j, the rest i − j is either kept whole, giving j × (i − j), or split further, giving j × best[i − j].
Common pitfalls
Cutting threes until 3 or less is left
while n > 3:
while n > 4:
That loop cuts a 3 from a remaining 4 and leaves a part of 1. On n = 10 it returns 3 × 3 × 3 × 1 = 27; the answer is 3 × 3 × 4 = 36.
Returning n for 2 and 3
if n <= 3:
return nif n <= 3:
return n - 1The number must be split into at least two parts. For n = 3 the best is 1 × 2 = 2, not 3.
In the table version, never keeping the rest whole
best[i] = max(best[i], j * best[i - j])
best[i] = max(best[i], j * (i - j), j * best[i - j])
best[k] is the product after splitting k, and every split starts from best[1] = 0, so without the j * (i - j) option every entry stays 0. Even when seeded, it misses that a leftover 2 or 3 is worth more whole than split.
Complexity
Integer Break FAQ
What if n is huge and the answer is asked modulo 10⁹ + 7?
The rule about threes still holds, but the loop is too slow and the product overflows. Write n = 3k + r, then take 3^k with fast power under the modulus, and fix the end: for r = 1 use 3^(k − 1) × 4, for r = 2 use 3^k × 2. The table version cannot be used at that size.
Is this the same as rod cutting?
Both cut a length into pieces, but rod cutting gives each piece length a price from a table and adds the prices. Here a piece is worth its own length and the values are multiplied, which is why a fixed best size, 3, exists.
Why is 3 the best size and not e, about 2.718?
With real numbers allowed, equal parts of size e give the largest product. Parts here must be whole numbers. A part of size p multiplies the product by p for every p units it uses, so per unit it is worth p^(1/p). Among whole numbers that is largest at 3 (about 1.442), just ahead of 2 (about 1.414).