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
10 = 4 + 3 + 3 and 4 * 3 * 3 = 36
is maximum possible product.
Input : n = 8
Output : 18
8 = 2 + 3 + 3 and 2 * 3 * 3 = 18
is 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:
C++
#include <bits/stdc++.h>
using namespace std;
int power( int x, int a)
{
int res = 1;
while (a) {
if (a & 1)
res = res * x;
x = x * x;
a >>= 1;
}
return res;
}
int breakInteger( int N)
{
if (N == 2)
return 1;
if (N == 3)
return 2;
int maxProduct;
switch (N % 3) {
case 0:
maxProduct = power(3, N / 3);
break ;
case 1:
maxProduct = 2 * 2 * power(3, (N / 3) - 1);
break ;
case 2:
maxProduct = 2 * power(3, N / 3);
break ;
}
return maxProduct;
}
int main()
{
int maxProduct = breakInteger(10);
cout << maxProduct << endl;
return 0;
}
|
Java
class GFG {
static int power( int x, int a)
{
int res = 1 ;
while (a > 0 ) {
if ((a & 1 ) > 0 )
res = res * x;
x = x * x;
a >>= 1 ;
}
return res;
}
static int breakInteger( int N)
{
if (N == 2 )
return 1 ;
if (N == 3 )
return 2 ;
int maxProduct = - 1 ;
switch (N % 3 ) {
case 0 :
maxProduct = power( 3 , N / 3 );
break ;
case 1 :
maxProduct = 2 * 2 * power( 3 , (N / 3 ) - 1 );
break ;
case 2 :
maxProduct = 2 * power( 3 , N / 3 );
break ;
}
return maxProduct;
}
public static void main(String[] args)
{
int maxProduct = breakInteger( 10 );
System.out.println(maxProduct);
}
}
|
Python3
def power(x, a):
res = 1
while (a):
if (a & 1 ):
res = res * x
x = x * x
a >> = 1
return res
def breakInteger(N):
if (N = = 2 ):
return 1
if (N = = 3 ):
return 2
maxProduct = 0
if (N % 3 = = 0 ):
maxProduct = power( 3 , int (N / 3 ))
return maxProduct
else if (N % 3 = = 1 ):
maxProduct = 2 * 2 * power( 3 , int (N / 3 ) - 1 )
return maxProduct
else if (N % 3 = = 2 ):
maxProduct = 2 * power( 3 , int (N / 3 ))
return maxProduct
maxProduct = breakInteger( 10 )
print (maxProduct)
|
C#
class GFG {
static int power( int x, int a)
{
int res = 1;
while (a > 0) {
if ((a & 1) > 0)
res = res * x;
x = x * x;
a >>= 1;
}
return res;
}
static int breakInteger( int N)
{
if (N == 2)
return 1;
if (N == 3)
return 2;
int maxProduct = -1;
switch (N % 3) {
case 0:
maxProduct = power(3, N / 3);
break ;
case 1:
maxProduct = 2 * 2 * power(3, (N / 3) - 1);
break ;
case 2:
maxProduct = 2 * power(3, N / 3);
break ;
}
return maxProduct;
}
public static void Main()
{
int maxProduct = breakInteger(10);
System.Console.WriteLine(maxProduct);
}
}
|
Javascript
<script>
function power(x, a)
{
let res = 1;
while (a > 0)
{
if ((a & 1) > 0)
res = res * x;
x = x * x;
a >>= 1;
}
return res;
}
function breakInteger(N)
{
if (N == 2)
return 1;
if (N == 3)
return 2;
let maxProduct;
switch (N % 3)
{
case 0:
maxProduct = power(3, N / 3);
break ;
case 1:
maxProduct = 2 * 2 * power(3, (N / 3) - 1);
break ;
case 2:
maxProduct = 2 * power(3, N / 3);
break ;
}
return maxProduct;
}
let maxProduct = breakInteger(10);
document.write(maxProduct);
</script>
|
PHP
<?php
function power( $x , $a )
{
$res = 1;
while ( $a )
{
if ( $a & 1)
$res = $res * $x ;
$x = $x * $x ;
$a >>= 1;
}
return $res ;
}
function breakInteger( $N )
{
if ( $N == 2)
return 1;
if ( $N == 3)
return 2;
$maxProduct =0;
switch ( $N % 3)
{
case 0:
$maxProduct = power(3, $N /3);
break ;
case 1:
$maxProduct = 2 * 2 * power(3, ( $N /3) - 1);
break ;
case 2:
$maxProduct = 2 * power(3, $N /3);
break ;
}
return $maxProduct ;
}
$maxProduct = breakInteger(10);
echo $maxProduct ;
?>
|
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.
C++
#include <iostream>
using namespace std;
int maxProd( int n)
{
if (n == 2 || n == 3) return (n-1);
int res = 1;
while (n > 4)
{
n -= 3;
res *= 3;
}
return (n * res);
}
int main()
{
cout << "Maximum Product is " << maxProd(45);
return 0;
}
|
Java
public class GFG
{
static int maxProd( int n)
{
if (n == 2 || n == 3 ) return (n - 1 );
int res = 1 ;
while (n > 4 )
{
n -= 3 ;
res *= 3 ;
}
return (n * res);
}
public static void main(String[] args) {
System.out.println( "Maximum Product is " + maxProd( 45 ));
}
}
|
Python3
def maxProd(n):
if (n = = 2 or n = = 3 ):
return (n - 1 );
res = 1 ;
while (n > 4 ):
n - = 3 ;
res * = 3 ;
return (n * res);
if __name__ = = '__main__' :
print ( "Maximum Product is" , maxProd( 45 ))
|
C#
using System;
class GFG {
static int maxProd( int n)
{
if (n == 2 || n == 3) return (n - 1);
int res = 1;
while (n > 4)
{
n -= 3;
res *= 3;
}
return (n * res);
}
static void Main()
{
Console.WriteLine( "Maximum Product is " + maxProd(45));
}
}
|
Javascript
<script>
function maxProd(n)
{
if (n == 2 || n == 3) return (n - 1);
let res = 1;
while (n > 4)
{
n -= 3;
res *= 3;
}
return (n * res);
}
document.write( "Maximum Product is " + maxProd(45));
</script>
|
OutputMaximum 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))
C++
#include <iostream>
using namespace std;
int helper( int n, int idx)
{
if (n == 0 or idx == 0) return 1;
if (idx > n) return helper(n, idx - 1);
return max((idx * helper(n - idx, idx)), helper(n , idx - 1));
}
int maxProd( int n)
{
return helper(n, n - 1);
}
int main()
{
cout << "Maximum Product is " << maxProd(45);
return 0;
}
|
Java
public class GFG {
static int helper( int n, int idx) {
if (n == 0 || idx == 0 )
return 1 ;
if (idx > n)
return helper(n, idx - 1 );
return Math.max((idx * helper(n - idx, idx)), helper(n, idx - 1 ));
}
static int maxProd( int n) {
return helper(n, n - 1 );
}
public static void main(String[] args) {
System.out.println( "Maximum Product is " + maxProd( 45 ));
}
}
|
Javascript
function helper(n, idx) {
if (n === 0 || idx === 0) return 1;
if (idx > n) return helper(n, idx - 1);
return Math.max(idx * helper(n - idx, idx), helper(n, idx - 1));
}
function maxProd(n) {
return helper(n, n - 1);
}
console.log( "Maximum Product is " + maxProd(45));
|
OutputMaximum Product is 14348907
Time Complexity: O(n),fo making the recursive calls.
Auxiliary Space: O(n), recursive stack space
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.