
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ find four factors of N with maximum product and sum equal to N .
Concept
With respect of a given integer N, our task is to determine all factors of N 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 find 4 such factors then print “Not possible”.
It should be noted that all the four factors can be equal to each other to maximize the product.
Input
24
Output
All the factors are -> 1 2 4 5 8 10 16 20 40 80 Product is -> 160000
Select the factor 20 four times,
Therefore, 20+20+20+20 = 24 and product is maximum.
Method
Following is the step by step algorithm to solve this problem −
- At first determine the factors of a number ‘N’ by visiting from 1 to square root of ‘N’ and verify if ‘i’ and ‘n/i’ divides N and store them in a vector.
- Now we sort the vector and print every element.
- Determine three numbers to maximize the product with the fourth number, implementing three loops.
- Finally we replace the next maximum product with the previous product.
- Print the product when you’ll find the four factors.
Example
// 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 findfactors2(int n1){ vector<int> vec2; // Now inserting all the factors in a vector s for (int i = 1; i * i <= n1; i++) { if (n1 % i == 0) { vec2.push_back(i); vec2.push_back(n1 / i); } } // Used to sort the vector sort(vec2.begin(), vec2.end()); // Used to print all the factors cout << "All the factors are -> "; for (int i = 0; i < vec2.size(); i++) cout << vec2[i] << " "; cout << endl; // Now any elements is divisible by 1 int maxProduct2 = 1; bool flag2 = 1; // implementing three loop we'll find // the three maximum factors for (int i = 0; i < vec2.size(); i++) { for (int j = i; j < vec2.size(); j++) { for (int k = j; k < vec2.size(); k++) { // Now storing the fourth factor in y int y = n1 - vec2[i] - vec2[j] - vec2[k]; // It has been seen that if the fouth factor become negative // then break if (y <= 0) break; // Now we will replace more optimum number // than the previous one if (n1 % y == 0) { flag2 = 0; maxProduct2 = max(vec2[i] * vec2[j] * vec2[k] *y,maxProduct2); } } } } // Used to print the product if the numbers exist if (flag2 == 0) cout << "Product is -> " << maxProduct2 << endl; else cout << "Not possible" << endl; } // Driver code int main(){ int n1; n1 = 80; findfactors2(n1); return 0; }
Output
All the factors are -> 1 2 4 5 8 10 16 20 40 80 Product is -> 160000
- Related Articles
- Find four factors of N with maximum product and sum equal to N in C++
- Find four factors of N with maximum product and sum equal to N - Set-2 in C++
- Find four factors of N with maximum product and sum equal to N - Set-2 in Python
- Find four factors of N with maximum product and sum equal to N - Set-2 in Python Program
- Find maximum product of digits among numbers less than or equal to N in C++
- Find N integers with given difference between product and sum in C++
- Maximum Primes whose sum is equal to given N in C++
- C++ program to find two numbers with sum and product both same as N
- Maximum GCD of N integers with given product in C++
- Find two numbers with sum and product both same as N in C++
- Find two numbers with sum and product both same as N in C++ Program
- Minimum number of power terms with sum equal to n using C++.
- Maximum sum of distinct numbers with LCM as N in C++
- Find a Number X whose sum with its digits is equal to N in C++
- Maximum circular subarray sum in C++\n

Advertisements