VOOZH about

URL: https://www.geeksforgeeks.org/dsa/breaking-integer-to-get-maximum-product/

⇱ Breaking an Integer to get Maximum Product - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Breaking an Integer to get Maximum Product

Last Updated : 23 Jul, 2025

Given a number n, the task is to break n in such a way that multiplication of its parts is maximized. 

Input : n = 10
Output: 36
Explanation: 10 = 4 + 3 + 3 and 4 * 3 * 3 = 36 is the maximum possible product.

Input: n = 8
Output: 18
Explanation: 8 = 2 + 3 + 3 and 2 * 3 * 3 = 18 is the maximum possible product.

Mathematically, we are given n and we need to maximize a1 * a2 * a3 …. * aK such that n = a1 + a2 + a3 … + aK and a1, a2, ... ak > 0.
Note that we need to break given Integer in at least two parts in this problem for maximizing the product.

Method 1 - 

Now we know from maxima-minima concept that, If an integer need to break in two parts, then to maximize their product those part should be equal. Using this concept lets break n into (n/x) x's then their product will be x(n/x), now if we take derivative of this product and make that equal to 0 for maxima, we will get to know that value of x should be e (base of the natural logarithm) for maximum product. As we know that 2 < e < 3, so we should break every Integer into 2 or 3 only for maximum product. 
Next thing is 6 = 3 + 3 = 2 + 2 + 2, but 3 * 3 > 2 * 2 * 2, that is every triplet of 2 can be replaced with tuple of 3 for maximum product, so we will keep breaking the number in terms of 3 only, until number remains as 4 or 2, which we will be broken into 2*2 (2*2 > 3*1) and 2 respectively and we will get our maximum product. 
In short, procedure to get maximum product is as follows – Try to break integer in power of 3 only and when integer remains small (<5) then use brute force. 
The complexity of below program is O(log N), because of repeated squaring power method

Follow the below steps to implement the above idea:

  • Define a function breakInteger that takes an integer N as input and returns the maximum product that can be obtained by breaking N into a sum of positive integers.
    • Check for the two base cases:
      • If N is 2, return 1.
      • If N is 3, return 2.
    • Define a variable maxProduct to store the maximum product.
    • Determine the remainder of N when divided by 3:
                 a. If the remainder is 0, the maximum product is 3 raised to the power of N/3.
                 b. If the remainder is 1, the maximum product is 2 multiplied by 2 multiplied by 3 raised to the power of (N/3)-1.
                 c. If the remainder is 2, the maximum product is 2 multiplied by 3 raised to the power of N/3.
    • Return the value of maxProduct.

Below is the implementation of the above approach: 


Output
36

Method 2 - 

If we see some examples of this problems, we can easily observe following pattern.
The maximum product can be obtained be repeatedly cutting parts of size 3 while size is greater than 4, keeping the last part as size of 2 or 3 or 4. For example, n = 10, the maximum product is obtained by 3, 3, 4. For n = 11, the maximum product is obtained by 3, 3, 3, 2. Following is the implementation of this approach.


Output
Maximum Product is 14348907

Time Complexity: O(n)
Auxiliary Space: O(1)

Method 3: (Using Recursion)

Intuition:
Basically in this problem, We have to maximize the product of some integers which sums up to the given integer. Let's take an example of n = 5 and try to solve it. So, we can break 5 to 4,1 or 3,1,1 or 2,1,1,1 or 1,1,1,1,1. We can also break it instead to 3,2 or 1,2,2 or 1,1,1,2 and so on.After taking all these possibilities, [3,2] gives the max product which is 6. Basically we see that our problem is getting divided into subproblems. Thus we can make use of recursion to solve this. Further, if we want to solve for n = 6, we can make use of the previous max product value we got for n = 5 that is 6 to check possibilites for 6, so we can easily do memoization in our recursive solution to reduce our time complexity.

Approach:
The idea is that at every value, we can loop from 1 to n-1 for the first value (a). The remain value (b) has two options:
Keep the same, which means the product will be a * b
Or broken down, which means the product is: a * max(integerBreak(b))


Output
Maximum Product is 14348907

Time Complexity: O(n),fo making the recursive calls.
Auxiliary Space: O(n), recursive stack space



Comment
Article Tags:
Article Tags: