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 the maximum number of elements in 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 the most times (twice) are 2, 5, 10.Input: arr[] = {120, 15, 24, 63, 18}Â
Output: 3Â
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 in the array. The current integer is a factor of.
- Add the count to the rank list and the integers to the 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<bits/stdc++.h>using namespace std;Â
// Function to print the integers that divide // the maximum number of elements from the arrayvoid 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 codeint main(){Â Â Â Â vector<int>arr = {120, 15, 24, 63, 18};Â Â Â Â maximumFactor(arr);}Â
// This code is contributed by// Surendra_Gangwar |
Java
// Java implementation of the approach import java.util.*; class GFG{     // Function to print the integers that // divide the maximum number of // elements from the arraystatic void maximumFactor(int []arr){         // Initialize two lists to store     // rank and factors     int[] rank = new int[Arrays.stream(arr).max().getAsInt() + 1];    int[] factors = new int[Arrays.stream(arr).max().getAsInt() + 1];    int g = 0;         // Start from 2 till the maximum    // element in arr     for (int i = 2;              i <= Arrays.stream(arr).max().getAsInt(); i++)     {        // Initialize a variable to count        // the number of elements it is a         // factor of         int count = 0;        for (int j = 0; j < arr.length; j++)             if (arr[j] % i == 0)                count += 1;                         rank[g] = count;         factors[g] = i;        g++;    }         // Maximum rank in the rank list     int m = Arrays.stream(rank).max().getAsInt();    for (int i = 0; i < rank.length; i++)     {        // Print all the elements with rank m         if (rank[i] == m)             System.out.print(factors[i] + " ");     }} Â
// Driver codepublic static void main (String[] args) {Â Â Â Â int []arr = {120, 15, 24, 63, 18};Â Â Â Â maximumFactor(arr); }}Â
// This code is contributed by // chandan_jnu |
Python
# Python3 implementation of the approachÂ
# Function to print the integers that divide # the maximum number of elements from the arraydef 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 codearr = [120, 15, 24, 63, 18]Â
maximumFactor(arr) |
C#
// C# implementation of the approach using System;using System.Collections;using System.Linq;Â
class GFG{     // Function to print the integers that // divide the maximum number of // elements from the arraystatic void maximumFactor(int []arr){         // Initialize two lists to store     // rank and factors     int[] rank = new int[arr.Max() + 1];    int[] factors = new int[arr.Max() + 1];    int g = 0;         // Start from 2 till the maximum    // element in arr     for (int i = 2; i <= arr.Max(); i++)     {        // Initialize a variable to count        // the number of elements it is a         // factor of         int count = 0 ;        for (int j = 0; j < arr.Length; j++)             if (arr[j] % i == 0)                count += 1;                         rank[g]=count;         factors[g]=i;        g++;    }         // Maximum rank in the rank list     int m = rank.Max();    for (int i = 0; i < rank.Length; i++)     {        // Print all the elements with rank m         if ((int)rank[i] == m)             Console.Write(factors[i]+" ");     }} Â
// Driver codestatic void Main(){Â
int []arr = {120, 15, 24, 63, 18};maximumFactor(arr); }}Â
// This code is contributed by chandan_jnu |
PHP
<?php// PHP implementation of the approach Â
// Function to print the integers that // divide the maximum number of // elements from the arrayfunction maximumFactor($arr){         // Initialize two lists to store     // rank and factors     $rank = array();    $factors = array();         // Start from 2 till the maximum    // element in arr     for ($i = 2; $i <= max($arr); $i++)     {        // Initialize a variable to count        // the number of elements it is a         // factor of         $count = 0 ;        for ($j = 0; $j < sizeof($arr); $j++)             if ($arr[$j] % $i == 0)                $count += 1;                         array_push($rank, $count);         array_push($factors, $i);    }         // Maximum rank in the rank list     $m = max($rank);    for ($i = 0; $i < sizeof($rank); $i++)     {        // Print all the elements with rank m         if ($rank[$i] == $m)             echo $factors[$i], " ";     }} Â
// Driver code $arr = array(120, 15, 24, 63, 18);Â
maximumFactor($arr) Â
// This code is contributed by Ryuga?> |
Javascript
<script>Â
// Javascript implementation of the approachÂ
// Function to print the integers that divide // the maximum number of elements from the arrayfunction maximumFactor(arr){    // Initialize two lists     // to store rank and factors    var n = arr.length;    var rank = [];    var factors = [];     var max = arr.reduce((a,b)=> Math.max(a,b));         // Start from 2 till the maximum element in arr    for (var i = 2; i <= max; i++)    {        // Initialize a variable        // to count the number of elements         // it is a factor of        var count = 0;        for (var j = 0; j < n; j++)        {            if (arr[j] % i == 0)                count+= 1;            rank.push(count);            factors.push(i);        }              }                           // Maximum rank in the rank list    var m = rank.reduce((a,b)=>Math.max(a,b));    for (var i = 0; i < rank.length; i++)    {        // Print all the elements with rank m        if (rank[i] == m)            document.write( factors[i] + " ");    }         } Â
// Driver codevar arr = [120, 15, 24, 63, 18];maximumFactor(arr);Â
Â
</script> |
Output
3
Complexity Analysis:
- Time Complexity: O(n * max(arr)), where max(arr) is the largest element of the array arr.
- Auxiliary Space: O(n * max(arr))
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



