 GeeksforGeeks App
Open App Browser
Continue

# Find four factors of N with maximum product and sum equal to N

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.

Examples

```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:

1. First find the factors of a number ‘N’ by traversing from 1 to square root of ‘N’ and check if ‘i’ and ‘n/i’ divide N and store them in a vector.
2. Sort the vector and print every element.
3. Find three numbers to maximize the product with the fourth number, using three loops.
4. Replace the next maximum product with the previous product.
5. Print the product when you’ll find the four factors.

Below is the implementation of above approach:

## C++

 `// C++ program to find four factors of N``// with maximum product and sum equal to N``#include ` `using` `namespace` `std;` `// Function to find factors``// and to print those four factors``void` `findfactors(``int` `n)``{``    ``vector<``int``> vec;` `    ``// inserting all the factors in a vector s``    ``for` `(``int` `i = 1; i * i <= n; i++) {``        ``if` `(n % i == 0) {``            ``vec.push_back(i);``            ``vec.push_back(n / i);``        ``}``    ``}` `    ``// sort the vector``    ``sort(vec.begin(), vec.end());` `    ``// print all the factors``    ``cout << ``"All the factors are -> "``;``    ``for` `(``int` `i = 0; i < vec.size(); i++)``        ``cout << vec[i] << ``" "``;``    ``cout << endl;` `    ``// Any elements is divisible by 1``    ``int` `maxProduct = 1;``    ``bool` `flag = 1;` `    ``// using three loop we'll find``    ``// the three maximum factors``    ``for` `(``int` `i = 0; i < vec.size(); i++) {``        ``for` `(``int` `j = i; j < vec.size(); j++) {``            ``for` `(``int` `k = j; k < vec.size(); k++) {``                ``// storing the fourth factor in y``                ``int` `y = n - vec[i] - vec[j] - vec[k];` `                ``// if the fourth factor become negative``                ``// then break``                ``if` `(y <= 0)``                    ``break``;` `                ``// we will replace more optimum number``                ``// than the previous one``                ``if` `(n % y == 0) {``                    ``flag = 0;``                    ``maxProduct = max(vec[i] * vec[j] * vec[k] * y,``                                                    ``maxProduct);``                ``}``            ``}``        ``}``    ``}` `    ``// print the product if the numbers exist``    ``if` `(flag == 0)``        ``cout << ``"Product is -> "` `<< maxProduct << endl;` `    ``else``        ``cout << ``"Not possible"` `<< endl;``}` `// Driver code``int` `main()``{``    ``int` `n;``    ``n = 50;` `    ``findfactors(n);``    ` `    ``return` `0;``}`

## Java

 `import` `java.util.Collections;``import` `java.util.Vector;` `// Java program to find four factors of N``// with maximum product and sum equal to N``class` `GFG``{` `    ``// Function to find factors``    ``// and to print those four factors``    ``static` `void` `findfactors(``int` `n)``    ``{``        ``Vector vec = ``new` `Vector();` `        ``// inserting all the factors in a vector s``        ``for` `(``int` `i = ``1``; i * i <= n; i++)``        ``{``            ``if` `(n % i == ``0``)``            ``{``                ``vec.add(i);``                ``vec.add(n / i);``            ``}``        ``}` `        ``// sort the vector``        ``Collections.sort(vec);` `        ``// print all the factors``        ``System.out.println(``"All the factors are -> "``);``        ``for` `(``int` `i = ``0``; i < vec.size(); i++)``        ``{``            ``System.out.print(vec.get(i) + ``" "``);``        ``}``        ``System.out.println();` `        ``// Any elements is divisible by 1``        ``int` `maxProduct = ``1``;``        ``boolean` `flag = ``true``;` `        ``// using three loop we'll find``        ``// the three maximum factors``        ``for` `(``int` `i = ``0``; i < vec.size(); i++)``        ``{``            ``for` `(``int` `j = i; j < vec.size(); j++)``            ``{``                ``for` `(``int` `k = j; k < vec.size(); k++)``                ``{``                    ``// storing the fourth factor in y``                    ``int` `y = n - vec.get(i) - vec.get(j) -``                                    ``vec.get(k);` `                    ``// if the fourth factor become negative``                    ``// then break``                    ``if` `(y <= ``0``)``                    ``{``                        ``break``;``                    ``}` `                    ``// we will replace more optimum number``                    ``// than the previous one``                    ``if` `(n % y == ``0``)``                    ``{``                        ``flag = ``false``;``                        ``maxProduct = Math.max(vec.get(i) * vec.get(j) *``                                            ``vec.get(k) * y, maxProduct);``                    ``}``                ``}``            ``}``        ``}` `        ``// print the product if the numbers exist``        ``if` `(flag == ``false``)``        ``{``            ``System.out.println(``"Product is -> "` `+ maxProduct);``        ``}``        ``else``        ``{``            ``System.out.println(``"Not possible"``);``        ``}``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `n;``        ``n = ``50``;``        ``findfactors(n);``    ``}``}` `// This code is contributed by PrinciRaj1992`

## Python3

 `# Python 3 to find four factors``# of N with maximum product and``# sum equal to N` `# import every thing from``# math library``from` `math ``import` `*` `# Function to find factors``# and to print those four factors``def` `findfactors(n) :``    ``vec ``=` `[]` `    ``# inserting all the factors``    ``# in a list vec``    ``for` `i ``in` `range``(``1``, ``int``(sqrt(n)) ``+` `1``) :``        ``if` `n ``%` `i ``=``=` `0` `:``            ``vec.append(i)``            ``vec.append(n ``/``/` `i)` `    ``# sort the list``    ``vec.sort()``    ` `    ``print``(``"All the factors are -> "``,``                           ``end ``=` `"")``    ` `    ``# print all the factors``    ``for` `i ``in` `range``(``len``(vec)) :``        ``print``(vec[i], end ``=` `" "``)``    ``print``()` `    ``# Any elements is divisible by 1``    ``maxProduct ``=` `1``    ``flag ``=` `1` `    ``# using three loop we'll find``    ``# the three maximum factors``    ``for` `i ``in` `range``(``0``, ``len``(vec)) :``        ``for` `j ``in` `range``(i, ``len``(vec)) :``            ``for` `k ``in` `range``(j, ``len``(vec)) :` `                ``# storing the fourth factor in y``                ``y ``=` `n ``-` `vec[i] ``-` `vec[j] ``-` `vec[k]` `                ``# if the fourth factor become``                ``# negative then break``                ``if` `y <``=` `0` `:``                    ``break` `                ``# we will replace more optimum``                ``# number than the previous one``                ``if` `n ``%` `y ``=``=` `0` `:``                    ``flag ``=` `0``                    ``maxProduct ``=` `max``(vec[i] ``*` `vec[j] ``*``                                     ``vec[k] ``*` `y , maxProduct)` `    ``# print the product if the numbers exist``    ``if` `flag ``=``=` `0` `:``        ``print``(``"Product is - >"``, maxProduct)``    ``else` `:``        ``print``(``"Not possible"``)` `# Driver Code``if` `__name__ ``=``=` `"__main__"` `:``    ``n ``=` `50` `    ``# function calling``    ``findfactors(n)``    ` `# This code is contributed by ANKITRAI1`

## C#

 `// C# program to find four factors of N``// with maximum product and sum equal to N``using` `System;``using` `System.Collections.Generic;` `class` `GFG``{` `    ``// Function to find factors``    ``// and to print those four factors``    ``static` `void` `findfactors(``int` `n)``    ``{``        ``List<``int``> vec = ``new` `List<``int``>();` `        ``// inserting all the factors in a vector s``        ``for` `(``int` `i = 1; i * i <= n; i++)``        ``{``            ``if` `(n % i == 0)``            ``{``                ``vec.Add(i);``                ``vec.Add(n / i);``            ``}``        ``}` `        ``// sort the vector``        ``vec.Sort();` `        ``// print all the factors``        ``Console.WriteLine(``"All the factors are -> "``);``        ``for` `(``int` `i = 0; i < vec.Count; i++)``        ``{``            ``Console.Write(vec[i] + ``" "``);``        ``}``        ``Console.WriteLine();` `        ``// Any elements is divisible by 1``        ``int` `maxProduct = 1;``        ``Boolean flag = ``true``;` `        ``// using three loop we'll find``        ``// the three maximum factors``        ``for` `(``int` `i = 0; i < vec.Count; i++)``        ``{``            ``for` `(``int` `j = i; j < vec.Count; j++)``            ``{``                ``for` `(``int` `k = j; k < vec.Count; k++)``                ``{``                    ``// storing the fourth factor in y``                    ``int` `y = n - vec[i] - vec[j] -``                                         ``vec[k];` `                    ``// if the fourth factor become negative``                    ``// then break``                    ``if` `(y <= 0)``                    ``{``                        ``break``;``                    ``}` `                    ``// we will replace more optimum number``                    ``// than the previous one``                    ``if` `(n % y == 0)``                    ``{``                        ``flag = ``false``;``                        ``maxProduct = Math.Max(vec[i] * vec[j] *``                                              ``vec[k] * y, maxProduct);``                    ``}``                ``}``            ``}``        ``}` `        ``// print the product if the numbers exist``        ``if` `(flag == ``false``)``        ``{``            ``Console.WriteLine(``"Product is -> "` `+``                                    ``maxProduct);``        ``}``        ``else``        ``{``            ``Console.WriteLine(``"Not possible"``);``        ``}``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``int` `n;``        ``n = 50;``        ``findfactors(n);``    ``}``}` `// This code is contributed by Rajput-Ji`

## PHP

 ` "``;``    ``for` `(``\$i` `= 0; ``\$i` `< sizeof(``\$vec``); ``\$i``++)``        ``echo` `\$vec``[``\$i``] . ``" "``;``    ``echo` `"\n"``;` `    ``// Any elements is divisible by 1``    ``\$maxProduct` `= 1;``    ``\$flag` `= 1;` `    ``// using three loop we'll find``    ``// the three maximum factors``    ``for` `(``\$i` `= 0; ``\$i` `< sizeof(``\$vec``); ``\$i``++)``    ``{``        ``for` `(``\$j` `= ``\$i``;``             ``\$j` `< sizeof(``\$vec``); ``\$j``++)``        ``{``            ``for` `(``\$k` `= ``\$j``;``                 ``\$k` `< sizeof(``\$vec``); ``\$k``++)``            ``{``                ``// storing the fourth factor in y``                ``\$y` `= ``\$n` `- ``\$vec``[``\$i``] -``                          ``\$vec``[``\$j``] - ``\$vec``[``\$k``];` `                ``// if the fourth factor become``                ``// negative then break``                ``if` `(``\$y` `<= 0)``                    ``break``;` `                ``// we will replace more optimum``                ``// number than the previous one``                ``if` `(``\$n` `% ``\$y` `== 0)``                ``{``                    ``\$flag` `= 0;``                    ``\$maxProduct` `= max(``\$vec``[``\$i``] * ``\$vec``[``\$j``] *``                                      ``\$vec``[``\$k``] * ``\$y``, ``\$maxProduct``);``                ``}``            ``}``        ``}``    ``}` `    ``// print the product if``    ``// the numbers exist``    ``if` `(``\$flag` `== 0)``        ``echo` `"Product is -> "` `.``             ``\$maxProduct` `.``"\n"``;` `    ``else``        ``echo` `"Not possible"` `.``"\n"``;``}` `// Driver code``\$n` `= 50;``findfactors(``\$n``);``    ` `// This code is contributed``// by ChitraNayal``?>`

## Javascript

 ``

Output:

```All the factors are -> 1 2 5 10 25 50
Product is -> 12500```

Time Complexity: O(sqrt(n)+m3) where m is the number of factors of n.

Auxiliary Space: O(m)

My Personal Notes arrow_drop_up