# Maximum factors formed by two numbers

Given a list of N numbers, Find two numbers such that the product of the two numbers has the maximum number of factors. Formally given n numbers a0, a1, a2, …..an. We are required to find two numbers ai and aj such that their multiplication gives the maximum number of factors and return number of factors of ai * aj

Constraints

1 <= N <=100

1 <= ai <= 10^8

Examples:

Input : [4, 3, 8] Output : 8 3*4 = 12 which has 6 factors 3*8 = 24 which has 8 factors 4*8 = 32 which has 6 factors for ai, aj = {3, 8} we have the maximum number of factors 8

One simple approach seems to be to take two numbers with maximum prime factors. This approach might not work as the picked two numbers may have many common factors and their product might not have maximum factors. For example, in [4, 3, 8], (4, 8) is not answer, but 3, 8 is answer.

We consider every pair of numbers. For every pair, we find union of factors and finally return the pair which has maximum count in union.

## C++

`// C++ program to find the pair whose ` `// product has maximum factors. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `typedef` `unordered_map<` `int` `,` `int` `> u_map; ` ` ` `// Returns a map that has counts of individual ` `// factors in v[] ` `u_map countMap(vector<` `int` `> v) ` `{ ` ` ` `u_map map; ` ` ` `for` `(` `size_t` `i = 0; i < v.size(); i++) ` ` ` `if` `(map.find(v[i]) != map.end()) ` ` ` `map[v[i]]++; ` ` ` `else` ` ` `map.insert({v[i], 1}); ` ` ` `return` `map; ` `} ` ` ` `// Given two Numbers in the form of their ` `// prime factorized maps. Returns total ` `// number of factors of the product of ` `// those two numbers ` `int` `totalFactors(u_map m1, u_map m2) ` `{ ` ` ` `// Find union of all factors. ` ` ` `for` `(` `auto` `it = m2.begin(); it != m2.end(); ++it) { ` ` ` `if` `(m1.find(it->first) != m1.end()) ` ` ` `m1[it->first] += it->second; ` ` ` `else` ` ` `m1.insert({ it->first, it->second }); ` ` ` `} ` ` ` ` ` `int` `product = 1; ` ` ` `for` `(` `auto` `it = m1.begin(); it != m1.end(); it++) ` ` ` `product *= (it->second + 1); ` ` ` ` ` `return` `product; ` `} ` ` ` `// Prime factorization of a number is represented ` `// as an Unordered map ` `u_map primeFactorize(` `int` `n) ` `{ ` ` ` `vector<` `int` `> pfac; ` ` ` `int` `temp = 2; ` ` ` `while` `(temp * temp <= n) { ` ` ` `if` `(n % temp == 0) { ` ` ` `pfac.push_back(temp); ` ` ` `n = n / temp; ` ` ` `} ` ` ` `else` `{ ` ` ` `temp++; ` ` ` `} ` ` ` `} ` ` ` `if` `(n > 1) ` ` ` `pfac.push_back(n); ` ` ` ` ` `return` `countMap(pfac); ` `} ` ` ` `int` `maxProduct(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// vector containing of prime factorizations ` ` ` `// of every number in the array. Every element ` ` ` `// of vector contains factors and their counts. ` ` ` `vector<u_map> vum; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `vum.push_back(primeFactorize(arr[i])); ` ` ` ` ` `// Consider every pair and find the pair with ` ` ` `// maximum factors. ` ` ` `int` `maxp = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `for` `(` `int` `j = 0; j < n; j++) { ` ` ` `if` `(i == j) ` ` ` `continue` `; ` ` ` `maxp = max(maxp, ` ` ` `totalFactors(vum[i], vum[j])); ` ` ` `} ` ` ` `} ` ` ` `return` `maxp; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 4, 3, 8 }; ` ` ` `cout << maxProduct(arr, 3); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python

`# Python program to find the pair whose ` `# product has maximum factors. ` `from` `collections ` `import` `Counter ` ` ` `# Returns list of factors of n. ` `def` `prime_factorize(n): ` ` ` `primfac ` `=` `[] ` ` ` `d ` `=` `2` ` ` `while` `d ` `*` `d <` `=` `n: ` ` ` `while` `(n ` `%` `d) ` `=` `=` `0` `: ` ` ` `primfac.append(d) ` ` ` `n ` `/` `/` `=` `d ` ` ` `d ` `+` `=` `1` ` ` `if` `n > ` `1` `: ` ` ` `primfac.append(n) ` ` ` `return` `Counter(primfac) ` ` ` `# Returns total factors in c1 and c2 ` `def` `total_factors(c1, c2): ` ` ` ` ` `c ` `=` `c1 ` `+` `c2 ` ` ` `v ` `=` `c.values() ` ` ` ` ` `# calc product of each element + 1 ` ` ` `p ` `=` `1` ` ` `for` `i ` `in` `v: ` ` ` `p ` `*` `=` `(i ` `+` `1` `) ` ` ` `return` `p ` ` ` `def` `max_product(arr): ` ` ` `n ` `=` `len` `(arr) ` ` ` ` ` `# Loop through all the nc2 possibilities ` ` ` `pfac ` `=` `[] ` ` ` `for` `i ` `in` `arr: ` ` ` `pfac.append(prime_factorize(i)) ` ` ` `maxp ` `=` `0` ` ` `for` `i, v1 ` `in` `enumerate` `(arr): ` ` ` `for` `j, v2 ` `in` `enumerate` `(arr): ` ` ` `if` `i ` `=` `=` `j: ` ` ` `continue` ` ` `p ` `=` `total_factors(pfac[i], pfac[j]) ` ` ` `if` `(p>maxp): ` ` ` `maxp ` `=` `p ` ` ` `return` `maxp ` ` ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `print` `max_product([` `4` `, ` `8` `, ` `3` `]) ` |

*chevron_right*

*filter_none*

Output:

8

An **alternate approach **is to find LCM of all pairs and find factors in all LCMs. Finally return the pair whose LCM has maximum factors.

## Recommended Posts:

- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Find four factors of N with maximum product and sum equal to N
- Maximum number of unique prime factors
- 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 | Set 3
- Generation of n numbers with given set of factors
- Sum of all odd factors of numbers in the range [l, r]
- Sum of all even factors of numbers in the range [l, r]
- Common prime factors of two numbers
- Maximum possible time that can be formed from four digits
- Count numbers from range whose prime factors are only 2 and 3
- K-Primes (Numbers with k prime factors) in a range
- Count common prime factors of two numbers
- Maximum number of teams that can be formed with given persons
- Number of distinct prime factors of first n natural numbers

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.