Given an number n, the task is to broken 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.
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.
Below is the implementation of above approach:
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum decrements to make integer A divisible by integer B
- Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
- Count pairs in Array whose product is a Kth power of any positive integer
- Count of index pairs in array whose range product is a positive integer
- Maximum element in an array such that its previous and next element product is maximum
- Program for dot product and cross product of two vectors
- Smallest pair of indices with product of subarray co-prime with product of the subarray on the left or right
- Maximum value of an integer for which factorial can be calculated on a machine
- Biggest integer which has maximum digit sum in range from 1 to n
- Maximum positive integer divisible by C and is in the range [A, B]
- Find a pair (n,r) in an integer array such that value of nPr is maximum
- Find a pair (n,r) in an integer array such that value of nCr is maximum
- Find an integer that is common in the maximum number of given arithmetic progressions
- Find the maximum sum (a+b) for a given input integer N satisfying the given condition
- Find the largest number smaller than integer N with maximum number of set bits
- Minimum integer with at most K bits set such that their bitwise AND with N is maximum
- Maximum of sum and product of digits until number is reduced to a single digit
- Find four factors of N with maximum product and sum equal to N | Set-2
- Find four factors of N with maximum product and sum equal to N
- Find four factors of N with maximum product and sum equal to N | Set 3