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.
Input : N = 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 : N = 100 Output : 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:
The maximum product is 12500
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.
- 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
- Maximize the product of four factors of a Number
- Find number of factors of N when location of its two factors whose product is N is given
- Divide two integers without using multiplication, division and mod operator | Set2
- Queries to find whether a number has exactly four distinct factors or not
- Maximum number of prime factors a number can have with exactly x factors
- Check if a number exists having exactly N factors and K prime factors
- Find the row whose product has maximum count of prime factors
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Number less than equals to N with maximum product of prime factors
- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Sum of M maximum distinct digit sum from 1 to N that are factors of K
- Maximum possible time that can be formed from four digits
- Find Four points such that they form a square whose sides are parallel to x and y axes
- Find the number of ways to divide number into four parts such that a = c and b = d
- Minimise N such that sum of count of all factors upto N is greater than or equal to X
- Minimum sum obtained from groups of four elements from the given array
- Find the area of the shaded region formed by the intersection of four semicircles in a square
- Find the prime P using given four integers
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.