LeetCode #343 Medium

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.

  • n runs from 2 to 58. The largest answer, for 58, is 1,549,681,956, which still fits in a 32-bit int.
Constraints
  • 2 <= n <= 58
mathdynamic-programming
Open on LeetCode ↗
02

Intuition

Ask which part sizes can ever appear in a best split.

  • No part of 5 or more: a part p ≥ 5 can be cut into 2 and p − 2, and 2(p − 2) > p whenever p > 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.

How to spot this pattern

"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.

03

Approach

Try it first

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?

1

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.

2

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.

3

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.

4

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def integerBreak(self, n: int) -> int:
▶3 if n <= 3:
▶4 return n - 1
▶5 product = 1
▶6 while n > 4:
▶7 product *= 3
▶8 n -= 3
▶9 return product * n
length10 leftproduct1length 10, product starts at 1
n10
product1
Nothing has been cut yet. Parts of 3 are worth the most per unit of length, so the plan is to cut off 3s for as long as that never leaves a part of 1.
length37 leftproduct3=3more than 4 left → cut off a 3, 7 left
product3
n7
More than 4 units remained, so cutting a 3 leaves at least 2, never a lone 1. The product is multiplied by 3 and becomes 3.
length334 leftproduct3×3=9more than 4 left → cut off a 3, 4 left
product9
n4
More than 4 units remained, so cutting a 3 leaves at least 2, never a lone 1. The product is multiplied by 3 and becomes 9.
length334 leftproduct3×3=94 left: a 3 would leave a lone 1 → stop
n4
Only 4 is left. Cutting a 3 would leave a part of 1 and give 3 × 1 = 3. Keeping 4, which is the same as 2 × 2, gives 4, so the loop stops here.
length334product3×3×4=36answer: 3 × 3 × 4 = 36
answer36
Multiply in the last part. The split 3 + 3 + 4 = 10 is all 3s except for one last part of 2, 3 or 4, the shape the proof shows is best, so 36 is the largest product.
05

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].

▶1class Solution:
▶2 def integerBreak(self, n: int) -> int:
▶3 best = [0] * (n + 1)
▶4 for i in range(2, n + 1):
▶5 for j in range(1, i):
▶6 best[i] = max(best[i], j * (i - j), j * best[i - j])
▶7 return best[n]
06

Common pitfalls

Cutting threes until 3 or less is left

✗ Wrong
while n > 3:
✓ Right
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

✗ Wrong
if n <= 3:
    return n
✓ Right
if n <= 3:
    return n - 1

The 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

✗ Wrong
best[i] = max(best[i], j * best[i - j])
✓ Right
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.

07

Complexity

Time
O(n)
Space
O(1)
The loop runs about n / 3 times. Computing 3 to the power n / 3 with fast exponentiation makes it O(log n), which only matters for much larger n. The table version below is O(n²) time and O(n) space.
08

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).