- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

With respect of a given integer N, our task is to determine all factors of N and print the product of four factors of N so that −

- The sum of the four factors is equal to N.
- The product of the four factors is largest.

It has been seen that if it is impossible to determine 4 such factors then print “Not possible”. It should be noted thatall the four factors can be equal to each other to maximize the product.

N = 60

All the factors are -> 1 2 3 4 5 6 10 12 15 20 30 60 Product is -> 50625

Select the factor 15 four times,

Therefore, 15+15+15+15 = 60 and product is largest.

Here a method which takes a complexity of O(P^3), where P is the number of factors of N has been explained.

So an efficient method of time complexity O(N^2) can be obtained with the help of following steps.

- We store all the factors of given number in a container.
- Now we iterate for all pairs and store their sum in a different container.
- We have to mark the index (element1 + element2) with pair(element1, element2) to obtain the elements by which the sum was obtained.
- Again we iterate for all the pair_sums, and verify if n-pair_sum exists in the same container, as a result both the pairs form the quadruple.
- Implement the pair hash array to obtain the elements by which the pair was formed.
- Finally, store the largest of all such quadruples, and print it at the end.

// C++ program to find four factors of N // with maximum product and sum equal to N #include <bits/stdc++.h> using namespace std; // Shows function to find factors // and to print those four factors void findfactors1(int q){ vector<int> vec1; // Now inserting all the factors in a vector s for (int i = 1; i * i <= q; i++) { if (q % i == 0) { vec1.push_back(i); vec1.push_back(q / i); } } // Used to sort the vector sort(vec1.begin(), vec1.end()); // Used to print all the factors cout << "All the factors are -> "; for (int i = 0; i < vec1.size(); i++) cout << vec1[i] << " "; cout << endl; // So any elements is divisible by 1 int maxProduct1 = 1; bool flag1 = 1; // implementing three loop we'll find // the three largest factors for (int i = 0; i < vec1.size(); i++) { for (int j = i; j < vec1.size(); j++) { for (int k = j; k < vec1.size(); k++) { // Now storing the fourth factor in y int y = q - vec1[i] - vec1[j] - vec1[k]; // It has been seen that if the fouth factor become negative // then break if (y <= 0) break; // So we will replace more optimum number // than the previous one if (q % y == 0) { flag1 = 0; maxProduct1 = max(vec1[i] * vec1[j] * vec1[k] *y,maxProduct1); } } } } // Used to print the product if the numbers exist if (flag1 == 0) cout << "Product is -> " << maxProduct1 << endl; else cout << "Not possible" << endl; } // Driver code int main(){ int q; q = 60; findfactors1(q); return 0; }

All the factors are -> 1 2 3 4 5 6 10 12 15 20 30 60 Product is -> 50625

- Related Questions & Answers
- Find four factors of N with maximum product and sum equal to N - Set-2 in Python
- C++ 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-2 in Python Program
- Find four factors of N with maximum product and sum equal to N in C++
- C/C++ Program to Find sum of Series with n-th term as n power of 2 - (n-1) power of 2
- C/C++ Program to Find the sum of Series with the n-th term as n^2 – (n-1)^2
- Find maximum product of digits among numbers less than or equal to N in C++
- Java Program to Find sum of Series with n-th term as n^2 – (n-1)^2
- Find N integers with given difference between product and sum in C++
- Program to find sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2) in C++
- Python Program for Find sum of Series with the n-th term as n^2 – (n-1)^2
- Maximum GCD of N integers with given product in C++
- Find two numbers with sum and product both same as N in C++
- Maximum Primes whose sum is equal to given N in C++
- Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! in C++

Advertisements