Integer Break
Last updated
Last updated
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.
Example 1:
Example 2:
Note: You may assume that n is not less than 2 and not larger than 58.
: maximum product by splitting into at least 2 integers.
There are ways (exclusive) to split : split such that the numbers contain , .
Therefore,
Claims:
The factors (integers) must be either 2 or 3.
There are at most two 2s.
Proof:
For any factor f larger than 3, we can further split the factor f to produce a new product that is at least as good as the old product.
How to come up with claim 1?
Suppose we are allowed to split n into any rational numbers.
By the Inequality of Square-Arithmetic and Geometric Means, if we split n to k numbers, the product is maximized when the k numbers are equal.
References:
If f is even, split f to two f/2 s. , for .
If f is odd, split f into (f-1)/2 and (f+1)/2. , for .
If there are more than two 2s, then we can replace three 2s to two 3s to increase the product. That is because .
So we want to find . Let . It is easy to show that is maximized when . Therefore, the numbers are .
But we are only allowed to split n to integers. So we use the integers close to , i.e., 2 and 3.