Given an integer . The task is to find all factors of N print the product of four factors of N such that:
- Sum of the four factors is equal to N.
- Product of the four factors is maximum.
If it is not possible to find 4 such factors then print “Not possible”.
Note: All the four factors can be equal to each other to maximize the product.
Input : 24 Output : Product -> 1296 All factors are -> 1 2 3 4 6 8 12 24 Choose the factor 6 four times, Therefore, 6+6+6+6 = 24 and product is maximum. Input : 100 Output : Product -> 390625 All the factors are -> 1 2 4 5 10 10 20 25 50 100 Choose the factor 25 four times.
In the second example, the product will be maximum when the four factors will be equal to 25. The sum of four factors is equal to ‘N’. Though we can choose the same factor four times to maximize the product.
Below is the step by step algorithm to solve this problem:
- First find the factors of a number ‘N’ by traversing from 1 to square root of ‘N’ and check if ‘i’ and ‘n/i’ divides N and store them in a vector.
- Sort the vector and print every element.
- Find three numbers to maximize the product with the fourth number, using three loops.
- Replace the next maximum product with the previous product.
- Print the product when you’ll find the four factors.
Below is the implementaion of above approach:
All the factors are -> 1 2 5 10 25 50 Product is -> 12500
- 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 | Set 3
- Find maximum product of digits among numbers less than or equal to N
- Find the Number of Maximum Product Quadruples
- Find the number in a range having maximum product of the digits
- Find a pair with maximum product in array of Integers
- Product of factors of number
- Find three integers less than or equal to N such that their LCM is maximum
- Maximize the product of four factors of a Number
- Ways to express a number as product of two different factors
- Product of unique prime factors of a number
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Maximum factors formed by two numbers
- Maximum number of unique prime factors
- Smallest number k such that the product of digits of k is equal to n
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.