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

Given an integer N. 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.
- The 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 and there can be a large number of queries.

**Examples**:

Input: 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: 100Output: Product -> 390625 All the factors are -> 1 2 4 5 10 10 20 25 50 100 Choose the factor 25 four times.

The idea is to find factors of all numbers from 1 to N ( which is the maximum value of n ).

- An answer will be
**Not possible**if the given is prime. - And if the given n is divisible by 4 then answer will be pow(q, 4) where q is a quotient when n is divided by 4.
- If it is possible to find the answer then, the answer must include third last factor two times. And run a nested loop for other two factors.

Below is the implementation of the above approach:

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find primes ` `bool` `isPrime(` `int` `n) ` `{ ` ` ` `// Corner cases ` ` ` `if` `(n <= 1) ` ` ` `return` `false` `; ` ` ` `if` `(n <= 3) ` ` ` `return` `true` `; ` ` ` ` ` `// This is checked so that we can skip ` ` ` `// middle five numbers in below loop ` ` ` `if` `(n % 2 == 0 || n % 3 == 0) ` ` ` `return` `false` `; ` ` ` ` ` `for` `(` `int` `i = 5; i * i <= n; i = i + 6) ` ` ` `if` `(n % i == 0 || n % (i + 2) == 0) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Function to find factors ` `void` `factors(` `int` `N, vector<` `int` `>& v[]) ` `{ ` ` ` `for` `(` `int` `i = 2; i < N; i++) { ` ` ` ` ` `// run a loop upto square root of that number ` ` ` `for` `(` `int` `j = 1; j * j <= i; j++) { ` ` ` `if` `(i % j == 0) { ` ` ` ` ` `// if the n is perfect square ` ` ` `if` `(i / j == j) ` ` ` `v[i].push_back(j); ` ` ` ` ` `// otherwise push it's two divisors ` ` ` `else` `{ ` ` ` `v[i].push_back(j); ` ` ` `v[i].push_back(i / j); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// sort the divisors ` ` ` `sort(v[i].begin(), v[i].end()); ` ` ` `} ` `} ` ` ` `// Function to find max product ` `int` `product(` `int` `n) ` `{ ` ` ` `// To store factors of 'n' ` ` ` `vector<` `int` `> v[n + 100]; ` ` ` ` ` `// find factors ` ` ` `factors(n + 100, v); ` ` ` ` ` `// if it is divisible by 4. ` ` ` `if` `(n % 4 == 0) { ` ` ` `int` `x = n / 4; ` ` ` `x *= x; ` ` ` `return` `x * x; ` ` ` `} ` ` ` ` ` `else` `{ ` ` ` ` ` `// if it is prime ` ` ` `if` `(isPrime[n]) ` ` ` `return` `-1; ` ` ` ` ` `// otherwise answer will be possible ` ` ` `else` `{ ` ` ` `int` `ans = -1; ` ` ` `if` `(v[n].size() > 2) { ` ` ` ` ` `// include last third factor ` ` ` `int` `fac = v[n][v[n].size() - 3]; ` ` ` ` ` `// nested loop to find other two factors ` ` ` `for` `(` `int` `i = v[n].size() - 1; i >= 0; i--) { ` ` ` `for` `(` `int` `j = v[n].size() - 1; j >= 0; j--) { ` ` ` `if` `((fac * 2) + (v[n][j] + v[n][i]) == n) ` ` ` `ans = max(ans, fac * fac * v[n][j] * v[n][i]); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `ans; ` ` ` `} ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `n = 24; ` ` ` ` ` `// function call ` ` ` `cout << product(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1296

## Recommended Posts:

- 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 number of factors of N when location of its two factors whose product is N is given
- 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
- Queries to find maximum product pair in range with updates
- Maximize the product of four factors of a Number
- Distinct Prime Factors of Array Product
- Product of unique prime factors of a number
- Ways to express a number as product of two different factors
- Count numbers in a range having GCD of powers of prime factors equal to 1

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.