Integer Break solution explained
Integer Break asks you to split a positive integer n (at least 2) into two or more positive parts with the largest possible product. A dynamic programming table solves it in O(n²): the best product for i is the maximum, over the first part j, of j times either the rest left whole or the rest's own best split. The faster answer uses as many 3s as possible: if n leaves remainder 1 when divided by 3, swap one 3 + 1 for 2 + 2, and if it leaves remainder 2, add a single 2. Only n = 2 and n = 3 need special handling.
By Evin JohnPublished 7 min read
The problem, in plain terms
Take a whole number n of at least 2. Write it as a sum of two or more positive whole numbers, then multiply those numbers together. Among all such ways, return the largest product. The pieces do not need to be different, and their order does not matter.
| n | Best split | Product |
|---|---|---|
| 2 | 1 + 1 | 1 |
| 3 | 1 + 2 | 2 |
| 4 | 2 + 2 | 4 |
| 5 | 2 + 3 | 6 |
| 6 | 3 + 3 | 9 |
| 7 | 3 + 2 + 2 | 12 |
| 8 | 3 + 3 + 2 | 18 |
| 9 | 3 + 3 + 3 | 27 |
| 10 | 3 + 3 + 2 + 2 | 36 |
| 11 | 3 + 3 + 3 + 2 | 54 |
Notice the first two rows. For n = 3, keeping 3 whole would give 3, but the rule demands at least two pieces, so the answer is only 2. This small-case trap is the main thing interviewers check before asking about the formula.
Brute force: try every first piece
Pick the first piece, from 1 to n - 1. What remains can either stay whole or be broken further, whichever is larger. Recursing on that gives every possible split. The outer level is the only one forced to split, which is how the at-least-two-pieces rule is enforced.
def integer_break_brute(n):
def best(m): # m may stay whole or be split further
result = m
for first in range(1, m):
result = max(result, first * best(m - first))
return result
return max(first * best(n - first) for first in range(1, n))The inner function recomputes the same values many times, so its running time grows exponentially with n. Caching best(m) fixes that and leads straight to the table below.
Dynamic programming in O(n²)
Let best[i] be the largest product from splitting i into at least two pieces. To fill it, choose the first piece j. The rest, i - j, can stay as one piece, giving j × (i - j), or be split further, giving j × best[i - j]. Take the maximum over every j and both options. The two options exist precisely because best[] always splits, and sometimes leaving the rest whole is better, as with 3.
def integer_break_dp(n):
best = [0] * (n + 1) # best[i] = largest product, i split into 2+ parts
best[1] = 1
for i in range(2, n + 1):
for j in range(1, i):
best[i] = max(best[i], j * (i - j), j * best[i - j])
return best[n]This is a perfectly good interview answer: O(n²) time, O(n) space, easy to justify. Many interviewers then ask whether you see a pattern in the table, which is the cue for the formula.
Why 3s win
A short exchange argument shows what an optimal split looks like for n of at least 4.
- No piece of 5 or more. Replace a piece p by 2 and p - 2. The product changes from p to 2p - 4, which is larger whenever p is above 4.
- 4 can be treated as 2 + 2. Both give a product of 4, so assume only 1s, 2s and 3s.
- No 1s. A piece of 1 multiplies by nothing. Adding it to any other piece x turns x × 1 into x + 1, which is bigger.
- At most two 2s. Three 2s (product 8) can become two 3s (product 9).
- So: use only 3s when n divides by 3; when the remainder is 1, use two 2s (4) plus 3s; when the remainder is 2, use one 2 plus 3s.
def integer_break(n):
if n <= 3:
return n - 1 # 2 -> 1 + 1, 3 -> 1 + 2
threes, rest = divmod(n, 3)
if rest == 0:
return 3 ** threes
if rest == 1:
return 3 ** (threes - 1) * 4 # trade 3 + 1 for 2 + 2
return 3 ** threes * 2Computing 3 to a power takes O(log n) multiplications by repeated squaring. Python's ** operator does this, though the numbers themselves grow, so for very large n the cost is in the big-integer arithmetic rather than the step count.
JavaScript and number size
The answer grows like 3^(n/3), which outruns JavaScript's safe integer range quickly. In our runs, the plain Number formula is above Number.MAX_SAFE_INTEGER from n = 101 on and first returns a wrong value at n = 102. If the interviewer allows any n, use BigInt.
function integerBreak(n) {
if (n <= 3) return BigInt(n - 1);
const threes = Math.floor(n / 3);
const rest = n % 3;
if (rest === 0) return 3n ** BigInt(threes);
if (rest === 1) return 3n ** BigInt(threes - 1) * 4n;
return 3n ** BigInt(threes) * 2n;
}A common follow-up asks for the answer modulo a large prime such as 1,000,000,007 when n is huge. The formula still works; replace each power with modular exponentiation (pow(3, threes, mod) in Python) and reduce after multiplying by 2 or 4. The DP does not work there, because comparing products that have been reduced modulo a prime tells you nothing about which product was larger.
Complexity summary
| Approach | Time | Extra space |
|---|---|---|
| Recursion over the first piece | Exponential in n | O(n) recursion |
| Recursion with a cache | O(n²) | O(n) |
| Bottom-up table | O(n²) | O(n) |
| Closed form with fast exponentiation | O(log n) multiplications | O(1) beyond the result |
Edge cases to test
- n = 2 and n = 3, where keeping n whole would be better but is not allowed.
- n = 4, the first case where 2 + 2 beats any split with a 3.
- One value for each remainder mod 3, such as 9, 10 and 11.
- A large n in JavaScript, to see whether your Number version silently loses precision.
Common mistakes
- Returning 3 for n = 3 or 2 for n = 2 because the code allows a single piece.
- In the DP, considering only j × best[i - j], which forgets that the rest may be best left whole. Seeded with best[1] = 1, that version returns 3 for i = 4 instead of 4.
- Applying the formula with remainder 1 as 3^q × 1, which wastes the 1. Swap one 3 + 1 for 2 + 2.
- Claiming O(1) time for the formula without mentioning the exponentiation or the size of the result.
- Reaching for the DP when the interviewer asks for the result modulo a prime, where comparisons no longer mean anything.
How to explain it out loud
Fictional example
- Candidate
- I'll start with a DP. best of i is the largest product splitting i into at least two pieces: try each first piece j, and the rest either stays whole or uses its own best. That's n squared.
- Candidate
- Looking at the table, the splits are all 3s with a 2 or two. Here's why: any piece 5 or bigger is beaten by 2 and p minus 2, 1s waste a slot, and three 2s lose to two 3s.
- Candidate
- So it's 3 to the n over 3, with a 4 or a 2 depending on the remainder. n equals 2 and 3 are special because I must split. In JavaScript I'd use BigInt for large n.
Related problems
Listing every way to reach a sum with a fixed number of distinct digits is Combination Sum III. Deciding whether values can be divided into groups of equal sum is Partition to K Equal Sum Subsets. Both are search problems, a useful contrast with how quickly this one collapses into a formula.
Natively for math-flavored problems
Problems like this one reward spotting a pattern and then proving it, and the proof is what interviewers remember. In practice sessions, Natively on Mac or Windows can read the problem from a screenshot and write up an approach and code, which you can check against your own table and proof. Afterward, the transcript shows whether you mentioned n = 2 and n = 3 before coding. Read about AI help for coding interview practice.
Questions people ask
How do you solve Integer Break?
Use as many 3s as possible. If n mod 3 is 0, the answer is 3^(n/3); if it is 1, replace one 3 + 1 with 2 + 2; if it is 2, multiply by one 2. Return 1 for n = 2 and 2 for n = 3.
Why are 3s optimal in Integer Break?
Any piece of 5 or more is beaten by splitting it into 2 and the rest, 1s waste a slot, and three 2s (8) lose to two 3s (9). That leaves 3s with at most two 2s.
What is the dynamic programming solution for Integer Break?
best[i] is the max over j of j × (i - j) and j × best[i - j], meaning the rest is left whole or split further. It runs in O(n²) time and O(n) space.
Why is Integer Break of 3 equal to 2?
The problem requires at least two pieces, so 3 cannot stay whole. The best split is 1 + 2, with product 2.
Where to go next
Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.
Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.
