Given an integer . The task is to find all factors of N and 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: N = 24Output: 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: N = 100Output: Product -> 390625 All the factors are -> 1 2 4 5 10 10 20 25 50 100 Choose the factor 25 four times.

An approach which takes a complexity of O(M^3), where M is the number of factors of N has been discussed in the previous post.

An efficient approach of time complexity O(N^2) can be obtained by following the below steps.

- Store all the factors of given number in a container.
- Iterate for all pairs and store their sum in a different container.
- Mark the index (element1 + element2) with pair(element1, element2 to get the elements by which the sum was obtained.
- Iterate for all the pair_sums, and check if n-pair_sum exists in the same container, then both the pairs form the quadruple.
- Use the pair hash array to get the elements by which the pair was formed.
- Store the maximum of all such quadruples, and print it at the end.

Below is the implementation of above approach:

`// C++ program to find four factors of N ` `// with maximum product and sum equal to N ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// Function to find factors ` `// and to print those four factors ` `void` `findfactors(` `int` `n) ` `{ ` ` ` `unordered_map<` `int` `, ` `int` `> mpp; ` ` ` ` ` `vector<` `int` `> v, v1; ` ` ` ` ` `// push all the factors in the container ` ` ` `for` `(` `int` `i = 1; i <= ` `sqrt` `(n); i++) { ` ` ` `if` `(n % i == 0) { ` ` ` `v.push_back(i); ` ` ` `if` `(i != (n / i) && i != 1) ` ` ` `v.push_back(n / i); ` ` ` `} ` ` ` `} ` ` ` ` ` `// number of factors ` ` ` `int` `s = v.size(); ` ` ` ` ` `// Initial maximum ` ` ` `int` `maxi = -1; ` ` ` ` ` `// hash-array to mark the ` ` ` `// pairs ` ` ` `pair<` `int` `, ` `int` `> mp1[n + 5]; ` ` ` ` ` `for` `(` `int` `i = 0; i < s; i++) { ` ` ` ` ` `// form all the pair sums ` ` ` `for` `(` `int` `j = i; j < s; j++) { ` ` ` ` ` `// if the pair sum is less than n ` ` ` `if` `(v[i] + v[j] < n) { ` ` ` ` ` `// push in another container ` ` ` `v1.push_back(v[i] + v[j]); ` ` ` ` ` `// mark the sum with the elements ` ` ` `// formed ` ` ` `mp1[v[i] + v[j]] = { v[i], v[j] }; ` ` ` ` ` `// mark in the map that v[i]+v[j] ` ` ` `// is present ` ` ` `mpp[v[i] + v[j]] = 1; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// new size of all the pair sums ` ` ` `s = v1.size(); ` ` ` ` ` `// iterate for all pair sum ` ` ` `for` `(` `int` `i = 0; i < s; i++) { ` ` ` ` ` `// the required part ` ` ` `int` `el = n - (v1[i]); ` ` ` ` ` `// if the required part is also ` ` ` `// present in pair sum ` ` ` `if` `(mpp[el] == 1) { ` ` ` ` ` `// find the elements with ` ` ` `// which the the first pair is formed ` ` ` `int` `a = mp1[v1[i]].first; ` ` ` `int` `b = mp1[v1[i]].second; ` ` ` ` ` `// find the elements with ` ` ` `// which the the second pair is formed ` ` ` `int` `c = mp1[n - v1[i]].first; ` ` ` `int` `d = mp1[n - v1[i]].second; ` ` ` ` ` `// check for previous maximum ` ` ` `maxi = max(a * b * c * d, maxi); ` ` ` `} ` ` ` `} ` ` ` ` ` `if` `(maxi == -1) ` ` ` `cout << ` `"Not Possible\n"` `; ` ` ` `else` `{ ` ` ` `cout << ` `"The maximum product is "` `<< maxi << endl; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 50; ` ` ` ` ` `findfactors(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

The maximum product is 12500

## Recommended Posts:

- Find four factors of N with maximum product and sum equal to N | Set 3
- Find four factors of N with maximum product and sum equal to N
- Find the Number of Maximum Product Quadruples
- Find three integers less than or equal to N such that their LCM is maximum
- Find a pair with maximum product in array of Integers
- Find the number in a range having maximum product of the digits
- Product of factors of number
- Maximize the product of four factors of a Number
- Distinct Prime Factors of Array Product
- 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
- Divide two integers without using multiplication, division and mod operator | Set2

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.