# Find integers that divides maximum number of elements of the array

Given an array arr[] of integers, the task is to find the element (other than 1) which is the factor of maximum number of elements from the array. If multiple such factors exist, print all the factors in ascending order.

Examples:

Input: arr[] = {10, 20}
Output: 2 5 10
The factors of 10 are 1, 2, 5, 10.
The factors of 20 are 1, 2, 4, 5, 10, 20.
The factors other than 1 which occur most number of times (twice) are 2, 5, 10.

Input: arr[] = {120, 15, 24, 63, 18}
Output: 3

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Initialize two lists one to store the rank (the number of elements that the integer is a factor of) of the factor and another to store the factor.
• Start from 2 till the maximum element of the array.
• Count the number of elements from the array the current integer is a factor of.
• Add the count to the rank list and the integer to factor list.
• Find the integer with the maximum rank.
• Print all the elements with the same rank.

Below is the implementation of the above approach:

## C++

 `// CPP implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to print the integers that divide  ` `// the maximum number of elements from the array ` `void` `maximumFactor(vector<``int``>arr) ` `{ ` `    ``// Initialize two lists  ` `    ``// to store rank and factors ` `    ``int` `n = arr.size(); ` `    ``vector<``int``> rank; ` `    ``vector<``int``> factors;  ` `    ``int` `max = *max_element(arr.begin(), arr.end());  ` `     `  `    ``// Start from 2 till the maximum element in arr ` `    ``for` `(``int` `i = 2; i <= max; i++) ` `    ``{ ` `        ``// Initialize a variable ` `        ``// to count the number of elements  ` `        ``// it is a factor of ` `        ``int` `count = 0; ` `        ``for` `(``int` `j = 0; j < n; j++) ` `        ``{ ` `            ``if` `(arr[j] % i == 0) ` `                ``count+= 1; ` `            ``rank.push_back(count); ` `            ``factors.push_back(i); ` `        ``}  ` `         `  `    ``} ` `         `  `         `  `     `  `    ``// Maximum rank in the rank list ` `    ``int` `m = *max_element(rank.begin(),rank.end()); ` `    ``for` `(``int` `i = 0; i < rank.size(); i++) ` `    ``{ ` `        ``// Print all the elements with rank m ` `        ``if` `(rank[i] == m) ` `            ``cout << factors[i] <<``" "``; ` `    ``} ` `         `  `}  ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``vector<``int``>arr = {120, 15, 24, 63, 18}; ` `    ``maximumFactor(arr); ` `} ` ` `  `// This code is contributed by ` `// Surendra_Gangwar `

## Python

 `# Python3 implementation of the approach ` ` `  `# Function to print the integers that divide  ` `# the maximum number of elements from the array ` `def` `maximumFactor(arr): ` `     `  `    ``# Initialize two lists  ` `    ``# to store rank and factors ` `    ``rank, factors ``=` `[], [] ` `     `  `    ``# Start from 2 till the maximum element in arr ` `    ``for` `i ``in` `range``(``2``, ``max``(arr)``+``1``): ` `         `  `        ``# Initialize a variable ` `        ``# to count the number of elements  ` `        ``# it is a factor of ` `        ``count ``=` `0` `        ``for` `j ``in` `arr: ` `            ``if` `j ``%` `i ``=``=` `0``:count``+``=` `1` `        ``rank.append(count) ` `        ``factors.append(i) ` `     `  `    ``# Maximum rank in the rank list ` `    ``m ``=` `max``(rank) ` `    ``for` `i ``in` `range``(``len``(rank)): ` `         `  `        ``# Print all the elements with rank m ` `        ``if` `rank[i]``=``=` `m: ` `            ``print``(factors[i], end ``=``" "``) ` ` `  `# Driver code ` `arr ``=` `[``120``, ``15``, ``24``, ``63``, ``18``] ` ` `  `maximumFactor(arr) `

## PHP

 ` `

Output:

```3
```

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.